11#!/usr/bin/env python3
22# coding=utf-8
3- """A simple example demonstrating how to use Argparse to support subcommands .
3+ """A simple example demonstrating how to use Argparse to support sub-commands .
44
55
6- This example shows an easy way for a single command to have many subcommands , each of which takes different arguments
6+ This example shows an easy way for a single command to have many sub-commands , each of which takes different arguments
77and provides separate contextual help.
88"""
99import argparse
1313
1414# create the top-level parser for the base command
1515base_parser = argparse .ArgumentParser (prog = 'base' )
16- base_subparsers = base_parser .add_subparsers (title = 'subcommands ' , help = 'subcommand help' )
16+ base_subparsers = base_parser .add_subparsers (title = 'sub-commands ' , help = 'sub-command help' )
1717
18- # create the parser for the "foo" subcommand
18+ # create the parser for the "foo" sub-command
1919parser_foo = base_subparsers .add_parser ('foo' , help = 'foo help' )
2020parser_foo .add_argument ('-x' , type = int , default = 1 , help = 'integer' )
2121parser_foo .add_argument ('y' , type = float , help = 'float' )
2222parser_foo .add_argument ('input_file' , type = str , help = 'Input File' )
2323
24- # create the parser for the "bar" subcommand
24+ # create the parser for the "bar" sub-command
2525parser_bar = base_subparsers .add_parser ('bar' , help = 'bar help' )
2626
2727bar_subparsers = parser_bar .add_subparsers (title = 'layer3' , help = 'help for 3rd layer of commands' )
3131bar_subparsers .add_parser ('artichoke' , help = 'artichoke help' )
3232bar_subparsers .add_parser ('cranberries' , help = 'cranberries help' )
3333
34- # create the parser for the "sport" subcommand
34+ # create the parser for the "sport" sub-command
3535parser_sport = base_subparsers .add_parser ('sport' , help = 'sport help' )
3636sport_arg = parser_sport .add_argument ('sport' , help = 'Enter name of a sport' )
3737setattr (sport_arg , 'arg_choices' , sport_item_strs )
4040# create the top-level parser for the alternate command
4141# The alternate command doesn't provide its own help flag
4242base2_parser = argparse .ArgumentParser (prog = 'alternate' , add_help = False )
43- base2_subparsers = base2_parser .add_subparsers (title = 'subcommands ' , help = 'subcommand help' )
43+ base2_subparsers = base2_parser .add_subparsers (title = 'sub-commands ' , help = 'sub-command help' )
4444
45- # create the parser for the "foo" subcommand
45+ # create the parser for the "foo" sub-command
4646parser_foo2 = base2_subparsers .add_parser ('foo' , help = 'foo help' )
4747parser_foo2 .add_argument ('-x' , type = int , default = 1 , help = 'integer' )
4848parser_foo2 .add_argument ('y' , type = float , help = 'float' )
4949parser_foo2 .add_argument ('input_file' , type = str , help = 'Input File' )
5050
51- # create the parser for the "bar" subcommand
51+ # create the parser for the "bar" sub-command
5252parser_bar2 = base2_subparsers .add_parser ('bar' , help = 'bar help' )
5353
5454bar2_subparsers = parser_bar2 .add_subparsers (title = 'layer3' , help = 'help for 3rd layer of commands' )
5858bar2_subparsers .add_parser ('artichoke' , help = 'artichoke help' )
5959bar2_subparsers .add_parser ('cranberries' , help = 'cranberries help' )
6060
61- # create the parser for the "sport" subcommand
61+ # create the parser for the "sport" sub-command
6262parser_sport2 = base2_subparsers .add_parser ('sport' , help = 'sport help' )
6363sport2_arg = parser_sport2 .add_argument ('sport' , help = 'Enter name of a sport' )
6464setattr (sport2_arg , 'arg_choices' , sport_item_strs )
6565
6666
6767class SubcommandsExample (cmd2 .Cmd ):
6868 """
69- Example cmd2 application where we a base command which has a couple subcommands
70- and the "sport" subcommand has tab completion enabled.
69+ Example cmd2 application where we a base command which has a couple sub-commands
70+ and the "sport" sub-command has tab completion enabled.
7171 """
7272 def __init__ (self ):
7373 super ().__init__ ()
7474
75- # subcommand functions for the base command
75+ # sub-command functions for the base command
7676 def base_foo (self , args ):
77- """foo subcommand of base command"""
77+ """foo sub-command of base command"""
7878 self .poutput (args .x * args .y )
7979
8080 def base_bar (self , args ):
81- """bar subcommand of base command"""
81+ """bar sub-command of base command"""
8282 self .poutput ('((%s))' % args .z )
8383
8484 def base_sport (self , args ):
85- """sport subcommand of base command"""
85+ """sport sub-command of base command"""
8686 self .poutput ('Sport is {}' .format (args .sport ))
8787
88- # Set handler functions for the subcommands
88+ # Set handler functions for the sub-commands
8989 parser_foo .set_defaults (func = base_foo )
9090 parser_bar .set_defaults (func = base_bar )
9191 parser_sport .set_defaults (func = base_sport )
@@ -95,21 +95,21 @@ def do_base(self, args):
9595 """Base command help"""
9696 func = getattr (args , 'func' , None )
9797 if func is not None :
98- # Call whatever subcommand function was selected
98+ # Call whatever sub-command function was selected
9999 func (self , args )
100100 else :
101- # No subcommand was provided, so call help
101+ # No sub-command was provided, so call help
102102 self .do_help ('base' )
103103
104104 @cmd2 .with_argparser (base2_parser )
105105 def do_alternate (self , args ):
106106 """Alternate command help"""
107107 func = getattr (args , 'func' , None )
108108 if func is not None :
109- # Call whatever subcommand function was selected
109+ # Call whatever sub-command function was selected
110110 func (self , args )
111111 else :
112- # No subcommand was provided, so call help
112+ # No sub-command was provided, so call help
113113 self .do_help ('alternate' )
114114
115115
0 commit comments