Python argparse

How I use argparse.

Basic main() stub

Argument default values

Use default= to set a default value for an argument.

To show default values in your help strings, use %(default)s interpolation:

If the default value is type str, it will be run through your type callback, otherwise it’s returned directly. This can be helpful when authoring help strings containing %(default)s:

Argument types

Use type= to set your own type conversion function. A type is just a function that converts a string to some output type.

Numbers.

Booleans. You can’t use type=bool for booleans (try running bool('True') in the REPL if you want to know why.) Do this instead:

Comma-separated list of values. How to specify a list of strings like python foo.py --tags=apple,fruit:

Subcommands

Add a subcommand called log: