diff options
author | Eric Lin <anselor@gmail.com> | 2018-04-25 11:40:33 -0400 |
---|---|---|
committer | Eric Lin <anselor@gmail.com> | 2018-04-25 11:40:33 -0400 |
commit | e018bbd79b2ddc0cf2538dff84baa8b2bf460ccd (patch) | |
tree | 4c35a13a67d0c1902e9d9a95be38f8da2d2f8160 /examples/subcommands.py | |
parent | 4193ef0344359d050bbfa469cbb898a52db97622 (diff) | |
download | cmd2-git-e018bbd79b2ddc0cf2538dff84baa8b2bf460ccd.tar.gz |
Removed the expensive imports from cmd2/__init__.py
Added some shared definitions to cmd2/__init__.py -> maybe there's a better place for these?
Figured out how to trick bash into showing argument hints. It's a bit weird.
Updated all of the tests and examples to import cmd2 resources from their new location without the automatic imports in cmd2/__init__.py
For #369
Diffstat (limited to 'examples/subcommands.py')
-rwxr-xr-x | examples/subcommands.py | 71 |
1 files changed, 42 insertions, 29 deletions
diff --git a/examples/subcommands.py b/examples/subcommands.py index 75c0733e..01f5b218 100755 --- a/examples/subcommands.py +++ b/examples/subcommands.py @@ -1,18 +1,56 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # coding=utf-8 +# PYTHON_ARGCOMPLETE_OK """A simple example demonstrating how to use Argparse to support subcommands. This example shows an easy way for a single command to have many subcommands, each of which takes different arguments and provides separate contextual help. """ +from cmd2.argcomplete_bridge import CompletionFinder +from cmd2.argparse_completer import AutoCompleter import argparse -import cmd2 -from cmd2 import with_argparser - sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball'] +# create the top-level parser for the base command +base_parser = argparse.ArgumentParser(prog='base') +base_subparsers = base_parser.add_subparsers(title='subcommands', help='subcommand help') + +# create the parser for the "foo" subcommand +parser_foo = base_subparsers.add_parser('foo', help='foo help') +parser_foo.add_argument('-x', type=int, default=1, help='integer') +parser_foo.add_argument('y', type=float, help='float') + +# create the parser for the "bar" subcommand +parser_bar = base_subparsers.add_parser('bar', help='bar help') + +bar_subparsers = parser_bar.add_subparsers(title='layer3', help='help for 3rd layer of commands') +parser_bar.add_argument('z', help='string') + +bar_subparsers.add_parser('apple', help='apple help') +bar_subparsers.add_parser('artichoke', help='artichoke help') +bar_subparsers.add_parser('cranberries', help='cranberries help') + +# create the parser for the "sport" subcommand +parser_sport = base_subparsers.add_parser('sport', help='sport help') +sport_arg = parser_sport.add_argument('sport', help='Enter name of a sport') +setattr(sport_arg, 'arg_choices', sport_item_strs) + +# Set both a function and tab completer for the "sport" subcommand + +if __name__ == '__main__': + with open('out.txt', 'a') as f: + f.write('Here 1') + f.flush() + completer = CompletionFinder() + completer(base_parser, AutoCompleter(base_parser)) + + +from cmd2 import cmd2 +from cmd2.cmd2 import with_argparser + + class SubcommandsExample(cmd2.Cmd): """ Example cmd2 application where we a base command which has a couple subcommands @@ -35,33 +73,8 @@ class SubcommandsExample(cmd2.Cmd): """sport subcommand of base command""" self.poutput('Sport is {}'.format(args.sport)) - # create the top-level parser for the base command - base_parser = argparse.ArgumentParser(prog='base') - base_subparsers = base_parser.add_subparsers(title='subcommands', help='subcommand help') - - # create the parser for the "foo" subcommand - parser_foo = base_subparsers.add_parser('foo', help='foo help') - parser_foo.add_argument('-x', type=int, default=1, help='integer') - parser_foo.add_argument('y', type=float, help='float') parser_foo.set_defaults(func=base_foo) - - # create the parser for the "bar" subcommand - parser_bar = base_subparsers.add_parser('bar', help='bar help') parser_bar.set_defaults(func=base_bar) - - bar_subparsers = parser_bar.add_subparsers(title='layer3', help='help for 3rd layer of commands') - parser_bar.add_argument('z', help='string') - - bar_subparsers.add_parser('apple', help='apple help') - bar_subparsers.add_parser('artichoke', help='artichoke help') - bar_subparsers.add_parser('cranberries', help='cranberries help') - - # create the parser for the "sport" subcommand - parser_sport = base_subparsers.add_parser('sport', help='sport help') - sport_arg = parser_sport.add_argument('sport', help='Enter name of a sport') - setattr(sport_arg, 'arg_choices', sport_item_strs) - - # Set both a function and tab completer for the "sport" subcommand parser_sport.set_defaults(func=base_sport) @with_argparser(base_parser) |