Skip to content

Additional command line support: argparse with cmd2 #21

@deadbits

Description

@deadbits

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')

Metadata

Metadata

Assignees

Labels

cliIssues about the interactive CLI workflow / errorsenhancementin-progess

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions