I have some old code which has optparse as their argument library. It has type as choice as one of the arguments
type = "choice"
I am trying to convert my code to have argparse. What should I put in the type? If I add type = choice it throws an error:
NameError: global name 'choice' is not defined
What should be the value of type here if I am using argparse?
In optparse
, choice
type is just a special form of string
. From the optparse
documenattion:
"choice"
options are a subtype of"string"
options. Thechoices
option attribute (a sequence of strings) defines the set of allowed option arguments.
You don't need to specify this as a type in argparse
; all you need to provide is the choices. Remove the type
argument altogether, or set it str
if you want to be explicit.
Also see the choices documentation for argparse
; this is a lot more flexible than in optparse
as this also allows for the choices to be of a type other than strings.