Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional command line support: argparse with cmd2 #21

Open
deadbits opened this issue Aug 27, 2018 · 1 comment
Open

Additional command line support: argparse with cmd2 #21

deadbits opened this issue Aug 27, 2018 · 1 comment
Assignees
Labels
cli Issues about the interactive CLI workflow / errors enhancement in-progess
Projects

Comments

@deadbits
Copy link
Contributor

By leveraging cmd2's support for argparse we can get rid of a lot of commands and add argument support to many existing ones. For example, we'll now have support for commands like: tags --view inquest.net, tags --add inquest.net these are my tags, https://inquest.net, artifact --new inquest.net, artifact --view inquest.net, etc etc.

The parsers will go in their own library that we import into omnibus-cli so it doesn't look like garbage inside that script. lets add them to lib/parsers.py

Example of the tags parser:

# do_tags parsere
tags_parser = argparse.ArgumentParser()
id_tags = tags_parser.add_mutually_exclusive_group()
manage_tags = tags_parser.add_mutually_exclusive_group()

manage_tags.add_argument('-a', '--add', action='store_true', help='add tag')
manage_tags.add_argument('-v', '--view', action='store_true', help='view tags')

id_tags.add_argument('-l', '--last', action='store_true', help='use last created artifact')
id_tags.add_argument('-n', '--name', action='store', help='artifact name')
id_tags.add_argument('-i', '--id', action='store', help='artifact session ID')

tags_parser.add_argument('tags', nargs='?', help='comma separated tags')

and we'd port tags to look something like this:

    @cmd2.with_argparser(tags_parser)
    def do_tags(self, args):
        """Manage artifact tags"""
        artifact = self.parse_identifier(args)
        if artifact is None:
            return

        _type = detect_type(artifact)

        if args.view:
            result = self.db.get_value(_type, {'name': artifact}, 'tags')
            info('%s tags: %s' % (artifact, result))
            return

        elif args.add:
            if args.tags == '':
                error('Tags not specified')
            else:
                tags = args.tags

                new_tags = []
                if ',' in tags:
                    for t in tags.split(','):
                        t = t.strip()
                        new_tags.append(t)
                else:
                    new_tags = tags

                if self.db.exists(_type, {'name': artifact}):
                    self.db.update_one(_type, {'name': artifact}, {'tags': new_tags})
                    success('Added tags to artifact (%s: %s)' % (artifact, new_tags))
                else:
                    warning('Failed to find artifact in MongoDB. Run "new <artifact name>" before using the tags command')

        else:
            info('Run "tags --help" or "help tags" for all available commands')
@deadbits deadbits added enhancement cli Issues about the interactive CLI workflow / errors labels Aug 27, 2018
@deadbits deadbits self-assigned this Aug 27, 2018
@deadbits deadbits added this to To do in Version 1.0 via automation Aug 27, 2018
@deadbits
Copy link
Contributor Author

Since many of the commands will accept artifacts as an argument, we'll also add a generic parser that will handle these more standard args of -l, --last for last created artifact, -n, --name for artifact by name, and -i, --id for artifact by session ID like seen in the last comment for tags.

Generic parser example

    def parse_identifier(self, args):
        """Parse artifact name out of argparse arguments"""
        identifier = None

        if args.last:
            if args.name or args.id:
                error('Only one argument can be specified: --last, --name, --id')
                return None

            identifier = self.session.receive('artifacts')

        elif args.id:
            is_key, value = lookup_key(self.session, args.id)

            if is_key and value is None:
                error('Unable to find artifact key in session (%s)' % args.id)
                return None

            elif is_key and value is not None:
                identifier = value

        elif args.name:
            identifier = args.name.strip()

        else:
            error('Must specify one of: --last, --name, --id')
            return None

        return identifier

@deadbits deadbits moved this from To do to Needs review in Version 1.0 Aug 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli Issues about the interactive CLI workflow / errors enhancement in-progess
Projects
Version 1.0
  
Testing
Development

No branches or pull requests

1 participant