ArgumentParser.
add_subparsers
([title][, description][, prog][, parser_class][, action][, option_string][, dest][, help][, metavar])- Many programs split up their functionality into a number of sub-commands, for example, the
svn
program can invoke sub-commands likesvn checkout
,svn update
, andsvn commit
. Splitting up functionality this way can be a particularly good idea when a program performs several different functions which require different kinds of command-line arguments.ArgumentParser
supports the creation of such sub-commands with theadd_subparsers()
method. Theadd_subparsers()
method is normally called with no arguments and returns a special action object. This object has a single method,add_parser()
, which takes a command name and anyArgumentParser
constructor arguments, and returns anArgumentParser
object that can be modified as usual.Description of parameters:- title - title for the sub-parser group in help output; by default “subcommands” if description is provided, otherwise uses title for positional arguments
- description - description for the sub-parser group in help output, by default None
- prog - usage information that will be displayed with sub-command help, by default the name of the program and any positional arguments before the subparser argument
- parser_class - class which will be used to create sub-parser instances, by default the class of the current parser (e.g. ArgumentParser)
- action - the basic type of action to be taken when this argument is encountered at the command line
- dest - name of the attribute under which sub-command name will be stored; by default None and no value is stored
- help - help for sub-parser group in help output, by default None
- metavar - string presenting available sub-commands in help; by default it is None and presents sub-commands in form {cmd1, cmd2, ..}
Some example usage:Note that the object returned byparse_args()
will only contain attributes for the main parser and the subparser that was selected by the command line (and not any other subparsers). So in the example above, when thea
command is specified, only thefoo
andbar
attributes are present, and when theb
command is specified, only thefoo
andbaz
attributes are present.Similarly, when a help message is requested from a subparser, only the help for that particular parser will be printed. The help message will not include parent parser or sibling parser messages. (A help message for each subparser command, however, can be given by supplying thehelp=
argument toadd_parser()
as above.)Theadd_subparsers()
method also supportstitle
anddescription
keyword arguments. When either is present, the subparser’s commands will appear in their own group in the help output. For example:One particularly effective way of handling sub-commands is to combine the use of theadd_subparsers()
method with calls toset_defaults()
so that each subparser knows which Python function it should execute. For example:This way, you can letparse_args()
do the job of calling the appropriate function after argument parsing is complete. Associating functions with actions like this is typically the easiest way to handle the different actions for each of your subparsers. However, if it is necessary to check the name of the subparser that was invoked, thedest
keyword argument to theadd_subparsers()
call will work:
https://killer.sh Pre Setup Once you've gained access to your terminal it might be wise to spend ~1 minute to setup your environment. You could set these: alias k = kubectl # will already be pre-configured export do = "--dry-run=client -o yaml" # k get pod x $do export now = "--force --grace-period 0" # k delete pod x $now Vim To make vim use 2 spaces for a tab edit ~/.vimrc to contain: set tabstop=2 set expandtab set shiftwidth=2 More setup suggestions are in the tips section . Question 1 | Contexts Task weight: 1% You have access to multiple clusters from your main terminal through kubectl contexts. Write all those context names into /opt/course/1/contexts . Next write a command to display the current context into /opt/course/1/context_default_kubectl.sh , the command should use kubectl . Finally write a second command doing the same thing into ...
Comments
Post a Comment
https://gengwg.blogspot.com/