blob: dd091db5da9483b22b8d5524f35c8bc38ba1e646 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
A simple argument parser for Python
Example usage:
import sys
import arg
def usage():
print('usage: %s [-v value] [-xy]' % sys.argv[0], file = sys.stderr)
sys.exit(1)
xflag = False
yflag = False
vflag = None
parser = arg.Parser(usage = usage)
for c in parser.flags:
if c == 'x':
xflag = True
elif c == 'y':
yflag = True
elif c == 'v':
vflag = parser.arg
else:
usage()
Supports:
Short flags without arguments
Short flags with optionally attached arguments
Joined short flags
Long flags with optional arguments
Long flags with mandatory arguments
Long flags with mandatory attached arguments
Long flags with mandatory detached arguments
Long flags with without arguments
Long flags with only one dash
Flags not starting with a dash
Numeral flags
Stop parsing flags at --
Mixing flags and arguments
Stop parsing flags at first argument
Returning -- as an argument
Not returning -- as an argument
|