Hide selected subcommands with argparse

I use argparse and set up subcommands for my program. I created subparameters to define these subcommands. I have some admin commands that should not be displayed to users on the help screen. I know that we could hide the arguments of the subcommand, but I do not know how we could hide some of the subcommands from the list in the help list.

Here is my piece of code,

parser = argparse.ArgumentParser(prog='myProg', description=desc, formatter_class=argparse.RawDescriptionHelpFormatter) subparsers = parser.add_subparsers(dest='sub_parser_name') myProg_query.add_subparser(subparsers) myProg_update.add_subparser(subparsers) myProg_configure.add_subparser(subparsers) myProg_result.add_subparser(subparsers) 

When I run the help command, I get this

 %> myProg --help usage: myProg [-h] positional arguments: {query,update,configure,result} query query information update Update configure Configure system result tabulate the result 

From the output of the help, I would like to display only the โ€œqueryโ€ and โ€œresultโ€ for the user. I tried using argparse.SUPPRESS in the add_subparser method, but it will hide all subcommands. Regardless of what I was looking for, we talked only about hiding the individual arguments of each subcommand, but not about hiding the subcommand. I may have to create my own formatting method, but I would like to check if there is another way to achieve this.

+6
source share
2 answers

metavar can do the trick:

 import argparse parser = argparse.ArgumentParser() sp = parser.add_subparsers(metavar='{cmd1,cmd2}') sp1 = sp.add_parser('cmd1') sp2 = sp.add_parser('cmd2') sp3 = sp.add_parser('cmd3') parser.parse_args() 

With this cmd3 not displayed in use or in help. But it appears in the error message

error: argument {cmd1, cmd2}: wrong choice: 'cmd' (choose from 'cmd1', 'cmd2', 'cmd3')


You may already have discovered this use of help=SUPPRESS . But this requires user-use parameters (and possibly descriptions):

 import argparse parser = argparse.ArgumentParser(usage='%(prog)s [-h] {cmd1,cmd2}') sp = parser.add_subparsers(help=argparse.SUPPRESS) sp1 = sp.add_parser('cmd1') sp2 = sp.add_parser('cmd2') sp3 = sp.add_parser('cmd3') parser.parse_args() 

In the main parser, subparameters look like choices positional argument. As far as I can tell, there is no way to selectively suppress choices .


At this level of question, learning the argparse.py code argparse.py may be more useful than docs. In this case, I looked at the code for class _SubParsersAction(Action) . This is doubly true if you want to configure the formatter. Existing alternative formats modify only one or two methods, deeply immersed in the class.


This issue was raised as a bug, http://bugs.python.org/issue22848 .

There is a patch that will change the display of choices based on help=SUPPRESS for individual subparameters. But I recommend the metavar solution, at least for now. There are other suggested fixes for working with choices .

+5
source

It looks like I found a solution to this problem without any patches for argparse. This is enough to change the "metavar" and not set the "help" for a specific subparameter.

 import argparse parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(title='commands', metavar='{command}') command = subparsers.add_parser("command", help='command help.', description='command description.') suppress_command = subparsers.add_parser("suppress_command", help=argparse.SUPPRESS, description='suppress command.') hidden_command = subparsers.add_parser("hidden_command", description='Hidden command.') 

The result is

 [ root@localhost ~]# parser -h usage: parser [-h] {command} ... optional arguments: -h, --help print help message and exit commands: {command} command command help. suppress_command ==SUPPRESS== [ root@localhost ~]# parser hidden_command -h usage: parser hidden_command [-h] Hidden command. optional arguments: -h, --help show this help message and exit 
+2
source

All Articles