diff options
author | Eric Lin <anselor@gmail.com> | 2018-10-06 18:15:23 +0000 |
---|---|---|
committer | Eric Lin <anselor@gmail.com> | 2018-10-06 18:15:23 +0000 |
commit | fb575e41251156a90044055e8a352079e4f75866 (patch) | |
tree | c8f013e04d7a9e28164367228fb0249e5b1dbbb2 /examples | |
parent | 6d79731ae51229d46263dcddfc945946afa6e238 (diff) | |
parent | 467be57e647112f536becc8625ffa080cb67a0ce (diff) | |
download | cmd2-git-fb575e41251156a90044055e8a352079e4f75866.tar.gz |
Merge remote-tracking branch 'origin/master' into argparse_remainder
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/subcommands.py | 42 | ||||
-rwxr-xr-x | examples/tab_autocomp_dynamic.py | 4 | ||||
-rwxr-xr-x | examples/tab_autocompletion.py | 8 | ||||
-rwxr-xr-x | examples/tab_completion.py | 2 |
4 files changed, 28 insertions, 28 deletions
diff --git a/examples/subcommands.py b/examples/subcommands.py index 02b06412..83c29393 100755 --- a/examples/subcommands.py +++ b/examples/subcommands.py @@ -1,9 +1,9 @@ #!/usr/bin/env python3 # coding=utf-8 -"""A simple example demonstrating how to use Argparse to support subcommands. +"""A simple example demonstrating how to use Argparse to support sub-commands. -This example shows an easy way for a single command to have many subcommands, each of which takes different arguments +This example shows an easy way for a single command to have many sub-commands, each of which takes different arguments and provides separate contextual help. """ import argparse @@ -13,15 +13,15 @@ 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') +base_subparsers = base_parser.add_subparsers(title='sub-commands', help='sub-command help') -# create the parser for the "foo" subcommand +# create the parser for the "foo" sub-command 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.add_argument('input_file', type=str, help='Input File') -# create the parser for the "bar" subcommand +# create the parser for the "bar" sub-command 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') @@ -31,7 +31,7 @@ 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 +# create the parser for the "sport" sub-command 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) @@ -40,15 +40,15 @@ setattr(sport_arg, 'arg_choices', sport_item_strs) # create the top-level parser for the alternate command # The alternate command doesn't provide its own help flag base2_parser = argparse.ArgumentParser(prog='alternate', add_help=False) -base2_subparsers = base2_parser.add_subparsers(title='subcommands', help='subcommand help') +base2_subparsers = base2_parser.add_subparsers(title='sub-commands', help='sub-command help') -# create the parser for the "foo" subcommand +# create the parser for the "foo" sub-command parser_foo2 = base2_subparsers.add_parser('foo', help='foo help') parser_foo2.add_argument('-x', type=int, default=1, help='integer') parser_foo2.add_argument('y', type=float, help='float') parser_foo2.add_argument('input_file', type=str, help='Input File') -# create the parser for the "bar" subcommand +# create the parser for the "bar" sub-command parser_bar2 = base2_subparsers.add_parser('bar', help='bar help') bar2_subparsers = parser_bar2.add_subparsers(title='layer3', help='help for 3rd layer of commands') @@ -58,7 +58,7 @@ bar2_subparsers.add_parser('apple', help='apple help') bar2_subparsers.add_parser('artichoke', help='artichoke help') bar2_subparsers.add_parser('cranberries', help='cranberries help') -# create the parser for the "sport" subcommand +# create the parser for the "sport" sub-command parser_sport2 = base2_subparsers.add_parser('sport', help='sport help') sport2_arg = parser_sport2.add_argument('sport', help='Enter name of a sport') setattr(sport2_arg, 'arg_choices', sport_item_strs) @@ -66,26 +66,26 @@ setattr(sport2_arg, 'arg_choices', sport_item_strs) class SubcommandsExample(cmd2.Cmd): """ - Example cmd2 application where we a base command which has a couple subcommands - and the "sport" subcommand has tab completion enabled. + Example cmd2 application where we a base command which has a couple sub-commands + and the "sport" sub-command has tab completion enabled. """ def __init__(self): super().__init__() - # subcommand functions for the base command + # sub-command functions for the base command def base_foo(self, args): - """foo subcommand of base command""" + """foo sub-command of base command""" self.poutput(args.x * args.y) def base_bar(self, args): - """bar subcommand of base command""" + """bar sub-command of base command""" self.poutput('((%s))' % args.z) def base_sport(self, args): - """sport subcommand of base command""" + """sport sub-command of base command""" self.poutput('Sport is {}'.format(args.sport)) - # Set handler functions for the subcommands + # Set handler functions for the sub-commands parser_foo.set_defaults(func=base_foo) parser_bar.set_defaults(func=base_bar) parser_sport.set_defaults(func=base_sport) @@ -95,10 +95,10 @@ class SubcommandsExample(cmd2.Cmd): """Base command help""" func = getattr(args, 'func', None) if func is not None: - # Call whatever subcommand function was selected + # Call whatever sub-command function was selected func(self, args) else: - # No subcommand was provided, so call help + # No sub-command was provided, so call help self.do_help('base') @cmd2.with_argparser(base2_parser) @@ -106,10 +106,10 @@ class SubcommandsExample(cmd2.Cmd): """Alternate command help""" func = getattr(args, 'func', None) if func is not None: - # Call whatever subcommand function was selected + # Call whatever sub-command function was selected func(self, args) else: - # No subcommand was provided, so call help + # No sub-command was provided, so call help self.do_help('alternate') diff --git a/examples/tab_autocomp_dynamic.py b/examples/tab_autocomp_dynamic.py index 2c90b7a2..af77b204 100755 --- a/examples/tab_autocomp_dynamic.py +++ b/examples/tab_autocomp_dynamic.py @@ -25,7 +25,7 @@ def query_actors() -> List[str]: class TabCompleteExample(cmd2.Cmd): - """ Example cmd2 application where we a base command which has a couple subcommands.""" + """ Example cmd2 application where we a base command which has a couple sub-commands.""" CAT_AUTOCOMPLETE = 'AutoComplete Examples' @@ -225,7 +225,7 @@ class TabCompleteExample(cmd2.Cmd): @cmd2.with_category(CAT_AUTOCOMPLETE) @cmd2.with_argparser(video_parser) def do_video(self, args): - """Video management command demonstrates multiple layers of subcommands being handled by AutoCompleter""" + """Video management command demonstrates multiple layers of sub-commands being handled by AutoCompleter""" func = getattr(args, 'func', None) if func is not None: # Call whatever subcommand function was selected diff --git a/examples/tab_autocompletion.py b/examples/tab_autocompletion.py index 7725be95..571b4082 100755 --- a/examples/tab_autocompletion.py +++ b/examples/tab_autocompletion.py @@ -25,7 +25,7 @@ def query_actors() -> List[str]: class TabCompleteExample(cmd2.Cmd): - """ Example cmd2 application where we a base command which has a couple subcommands.""" + """ Example cmd2 application where we a base command which has a couple sub-commands.""" CAT_AUTOCOMPLETE = 'AutoComplete Examples' @@ -275,7 +275,7 @@ class TabCompleteExample(cmd2.Cmd): @cmd2.with_category(CAT_AUTOCOMPLETE) @cmd2.with_argparser(video_parser) def do_video(self, args): - """Video management command demonstrates multiple layers of subcommands being handled by AutoCompleter""" + """Video management command demonstrates multiple layers of sub-commands being handled by AutoCompleter""" func = getattr(args, 'func', None) if func is not None: # Call whatever subcommand function was selected @@ -358,7 +358,7 @@ class TabCompleteExample(cmd2.Cmd): @cmd2.with_category(CAT_AUTOCOMPLETE) @cmd2.with_argparser(media_parser) def do_media(self, args): - """Media management command demonstrates multiple layers of subcommands being handled by AutoCompleter""" + """Media management command demonstrates multiple layers of sub-commands being handled by AutoCompleter""" func = getattr(args, 'func', None) if func is not None: # Call whatever subcommand function was selected @@ -459,7 +459,7 @@ class TabCompleteExample(cmd2.Cmd): @cmd2.with_category(CAT_AUTOCOMPLETE) @cmd2.with_argparser(library_parser) def do_library(self, args): - """Media management command demonstrates multiple layers of subcommands being handled by AutoCompleter""" + """Media management command demonstrates multiple layers of sub-commands being handled by AutoCompleter""" func = getattr(args, 'func', None) if func is not None: # Call whatever subcommand function was selected diff --git a/examples/tab_completion.py b/examples/tab_completion.py index 2ec7ff70..77d62988 100755 --- a/examples/tab_completion.py +++ b/examples/tab_completion.py @@ -12,7 +12,7 @@ sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball'] class TabCompleteExample(cmd2.Cmd): - """ Example cmd2 application where we a base command which has a couple subcommands.""" + """ Example cmd2 application where we a base command which has a couple sub-commands.""" def __init__(self): super().__init__() |