An option with an argument

We'll define an option that has an argument with the long and optional short name. We'll provide an action that stores the value provided with the argument. We can also provide a type conversion, in case we want float or int values instead of a string. Let's set this up using the following code:

parser.add_argument(
"-b", "--bet", action="store", default="Flat",
choices=["Flat", "Martingale", "OneThreeTwoSix"],
dest='betting_rule') parser.add_argument(
"-s", "--stake", action="store", default=50, type=int)

The first example will define two versions of the command-line syntax, both long and short version. When parsing the command-line argument values, a string value must follow the option, and it must be from the available choices. The destination name, betting_rule, will receive the option's argument string.

The second example also defines two versions of the command-line syntax. It includes a type conversion. When parsing argument values, this will store an integer value that follows the option. The long name, stake, will be the value in the options object created by the parser.

In some cases, there may be a list of values associated with the argument. In this case, we may provide a nargs="+" option to collect multiple values separated by spaces in a list.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset