This post contains some of my notes on the
What Arguments Were Passed
If you ever want to see what arguments were passed into a module, you can do the following:
import sys print(sys.argv)
Note that the first argument will be the path to the module, not an actual argument.
Required vs Optional Arguments
Arguments that start with
-- are optional arguments, while those that don’t are positional and therefore required (much like positional arguments in Python functions). Even though arguments starting with
-- are generally optional,
argparse still lets you mark them as required. This is considered bad design in most cases but isn’t prevented.
For example, you can do:
parser = argparse.ArgumentParser() parser.add_argument('--foo', required=True)
This is how it’s put in the documentation:
Required options are generally considered bad form because users expect options to be optional, and thus they should be avoided when possible.
Defaults for booleans
If you want an argument to default to true:
foo will be
--foo is added to the command, in which case it would become
If you want an argument to default to false:
This might be a little bit counter intuitive. Let’s think about a case where you want something to do true. Let’s say you want to include the training set in a train.
The common way to do this would be:
This means “if I add this tag, store true; otherwise, store false”. But that’s kind of like a double negative - leaving out the command to exclude the training set. Instead, let’s say you want to affirm it. You would do that like so:
parser.add_argument('--include_train', action='store_true') parser.add_argument('--include_val', action='store_true') parser.add_argument('--include_test', action='store_true')
This is probably better in this case. This means if I didn’t specify that set, don’t include it.