User Tools

Site Tools


argparse_notes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
argparse_notes [2024/02/12 22:01] rajuargparse_notes [2024/02/12 22:18] (current) raju
Line 1: Line 1:
-==== dummy 1 ==== 
-==== dummy 2 ==== 
-==== dummy 3 ==== 
-==== dummy 4 ==== 
 ==== --feature and --no-feature ==== ==== --feature and --no-feature ====
 +To do
 +<code>
 +command --feature
 +</code>
 +and
 +<code>
 +command --no-feature
 +</code>
 +
 +For python >= 3.9:
 +<code>
 +parser.add_argument('--feature', action=argparse.BooleanOptionalAction)
 +</code>
 +
 +For python < 3.9:
 +<code>
 +parser.add_argument('--feature', action='store_true')
 +parser.add_argument('--no-feature', dest='feature', action='store_false')
 +parser.set_defaults(feature=True)
 +</code>
 +
 +In practice:
 <code> <code>
 parser.add_argument( parser.add_argument(
Line 15: Line 33:
 parser.set_defaults(header=True) parser.set_defaults(header=True)
 </code> </code>
 +
 +Ref:-
 +  * https://stackoverflow.com/questions/15008758/parsing-boolean-values-with-argparse
 +  * https://docs.python.org/3/library/argparse.html#action -> search for ''<nowiki>--no-foo</nowiki>''
 +
argparse_notes.1707775276.txt.gz · Last modified: 2024/02/12 22:01 by raju