Skip to content

Commit 4c5a2d9

Browse files
committed
some extras
1 parent c04e176 commit 4c5a2d9

32 files changed

+506
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ tex2pdf*
99
.mypy_cache
1010
.log
1111
.coverage
12+
.idea

01_hello/hello04_argparse.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python3
2+
# Purpose: Say hello
3+
4+
import argparse
5+
6+
parser = argparse.ArgumentParser(description='Say hello')
7+
parser.add_argument('name', help='Whom to greet')
8+
args = parser.parse_args()
9+
name = args.name
10+
print('Hello, ' + name + '!')

02_crowsnest/crowsnest.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Me <me@foo.com>
4+
Date : today
5+
Purpose: Rock the Casbah
6+
"""
7+
8+
import argparse
9+
import os
10+
import sys
11+
12+
13+
# --------------------------------------------------
14+
def get_args():
15+
"""Get command-line arguments"""
16+
17+
parser = argparse.ArgumentParser(
18+
description='Rock the Casbah',
19+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
20+
21+
parser.add_argument('positional',
22+
metavar='str',
23+
help='A positional argument')
24+
25+
parser.add_argument('-a',
26+
'--arg',
27+
help='A named string argument',
28+
metavar='str',
29+
type=str,
30+
default='')
31+
32+
parser.add_argument('-i',
33+
'--int',
34+
help='A named integer argument',
35+
metavar='int',
36+
type=int,
37+
default=0)
38+
39+
parser.add_argument('-f',
40+
'--file',
41+
help='A readable file',
42+
metavar='FILE',
43+
type=argparse.FileType('r'),
44+
default=None)
45+
46+
parser.add_argument('-o',
47+
'--on',
48+
help='A boolean flag',
49+
action='store_true')
50+
51+
return parser.parse_args()
52+
53+
54+
# --------------------------------------------------
55+
def main():
56+
"""Make a jazz noise here"""
57+
58+
args = get_args()
59+
str_arg = args.arg
60+
int_arg = args.int
61+
file_arg = args.file
62+
flag_arg = args.on
63+
pos_arg = args.positional
64+
65+
print('str_arg = "{}"'.format(str_arg))
66+
print('int_arg = "{}"'.format(int_arg))
67+
print('file_arg = "{}"'.format(file_arg.name))
68+
print('flag_arg = "{}"'.format(flag_arg))
69+
print('positional = "{}"'.format(pos_arg))
70+
71+
72+
# --------------------------------------------------
73+
if __name__ == '__main__':
74+
main()
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)