Is it possible to remove or disable an argument in argparse, such that it does not show in the help? How?
It is easy to add new arguments:
parser = argparse.ArgumentParser() parser.add_argument('--arg1', help='Argument 1') parser.add_argument('--arg2', help='A second one')
And I know you can override arguments with a new definition by specifying the "resolve" conflict handler:
#In one script that should stand-alone and include arg1: parser = argparse.ArgumentParser(conflict_handler='resolve') parser.add_argument('--arg1', help='Argument 1') parser.add_argument('--arg2', help='A second one') #In another script with similar options parser.add_argument('--arg1', help='New number 1')
But this still includes arg1 in the help message and results of parse_args
Is there anything like
#Wishful thinking #In another script with similar options, that shouldn't include arg1 parser.remove_argument('--arg1')
Or another reasonably easy way to achieve this?
Also: Would the approach be different if the argument was a positional argument?
Note: the problem with removing arg1
after parsing as suggested here is that the argument still shows in the help
To add an optional argument, simply omit the required parameter in add_argument() . args = parser. parse_args()if args.
The remove() method takes a single element as an argument and removes it from the List. The item parameter is required, and any type (string, number, List) the element you want to remove. The remove() method only removes the given element from the List.
Number of Arguments If you want your parameters to accept a list of items you can specify nargs=n for how many arguments to accept. Note, if you set nargs=1 , it will return as a list not a single value.
>>> parser = argparse. ArgumentParser(description='Process some integers. ') The ArgumentParser object will hold all the information necessary to parse the command line into Python data types.
Is it possible to remove or disable an argument in argparse, such that it does not show in the help?
Set help
to argparse.SUPPRESS
when you add the argument, like this:
parser.add_argument('--arg1', help=argparse.SUPPRESS)
This will prevent the argument from showing up in the default help output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With