Abstract:
This page try to describe the basic function of argparse, which handle the input argument well.
argparse is included in python package. We can import it directly.
Content:
As it’s quite simple and useful for coding, I just give a short example to show the most common usage. You could add different types of arguments by add_argument
.
Basic Usage
1 | # import module |
Tricks
Usally, when we apply argpare, it means we what some input from command line. But it’s quite unconvinient in developing or debug process. Besides, if you want it could be both command line entrance or importable interface, argparse also provide this option.
1 | def main(arg_input=None): |
Now, you can either treat it as command line entrance and input argument from command line by invoke main()
, or you can use it as interface by transferring all the arguments to a function argument as string type, such as main("--list 1 2 -f")
.
History:
- 2016-09-11: 将内容记录下来
- 2016-11-06: finish the coding(can’t input Chinise now~~)
- 2019-02-06: add tricks to input from non command line