Skip to content

Commit 2c9a9e9

Browse files
committed
add python argparse
1 parent c735480 commit 2c9a9e9

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__

python/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# python
2+
3+
Code snippets for python.
4+
5+
6+
## todo
7+
8+
- user input
9+
- http get request
10+
- call external program
11+
- read file
12+

python/argparser.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python3
2+
"""
3+
argparse usage code snippet
4+
"""
5+
import argparse
6+
7+
def setup_argparse():
8+
"""
9+
:returns: argument parser object
10+
:rtype: argparse.ArgumentParser
11+
"""
12+
p = argparse.ArgumentParser(description="argparse example", prog="my-cli")
13+
14+
p.add_argument(
15+
"-n",
16+
"--name",
17+
type=str,
18+
help="the name",
19+
default="default-name",
20+
required=True,
21+
)
22+
23+
p.add_argument(
24+
"-c",
25+
"--count",
26+
type=int,
27+
help="the count",
28+
default=0,
29+
)
30+
31+
p.add_argument(
32+
"-f",
33+
"--fork",
34+
type=bool,
35+
help="fork as a daemon process",
36+
default=False,
37+
)
38+
39+
p.add_argument(
40+
"-i",
41+
"--interval",
42+
type=int,
43+
help="space-separated list of time intervals",
44+
nargs="+",
45+
)
46+
47+
return p
48+
49+
50+
if __name__ == "__main__":
51+
parser = setup_argparse()
52+
53+
args = parser.parse_args()
54+
print(args)

0 commit comments

Comments
 (0)