Skip to content

Commit 86d28de

Browse files
committed
more about argparse
1 parent e047d48 commit 86d28de

8 files changed

+79
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import argparse
2+
3+
parser = argparse.ArgumentParser(conflict_handler='resolve')
4+
parser.add_argument('-a', action='store')
5+
parser.add_argument('-b', action='store', help='short alone')
6+
parser.add_argument('--long-b', '-b', action='store', help='long and short together')
7+
print(parser.parse_args(['-h']))
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import argparse
2+
3+
parser = argparse.ArgumentParser(conflict_handler='resolve')
4+
parser.add_argument('-a', action='store')
5+
parser.add_argument('--long-b', '-b', action='store', help='long and short together')
6+
parser.add_argument('-b', action='store', help='short alone')
7+
print(parser.parse_args(['-h']))
8+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import argparse
2+
3+
parser = argparse.ArgumentParser(description='short sample app')
4+
parser.add_argument('--optional', action='store_true', default=False)
5+
parser.add_argument('positional', action='store')
6+
print(parser.parse_args())
7+
8+
# python argparse_default_grouping.py -h
9+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import argparse
2+
3+
parser = argparse.ArgumentParser()
4+
group = parser.add_mutually_exclusive_group()
5+
group.add_argument('-a', action='store_true')
6+
group.add_argument('-b', action='store_true')
7+
8+
print(parser.parse_args())
9+
10+
# python argparse_mutually_exclusive.py -a
11+
# python argparse_mutually_exclusive.py -b
12+
# python argparse_mutually_exclusive.py -a -b <- error
13+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import argparse
2+
3+
parser = argparse.ArgumentParser(add_help=False)
4+
group = parser.add_argument_group('authentication')
5+
group.add_argument('--user', action='store')
6+
group.add_argument('--password', action='store')
7+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import argparse
2+
3+
4+
parser = argparse.ArgumentParser()
5+
6+
subparsers = parser.add_subparsers(help='commands')
7+
8+
list_parser = subparsers.add_parser('list', help='list contents')
9+
list_parser.add_argument('dirname', action='store', help='directory to list')
10+
11+
create_parser = subparsers.add_parser('create', help='create a directory')
12+
create_parser.add_argument('driname', action='store', help='new directory to create')
13+
create_parser.add_argument('--readonly', default=False, action='store_true',
14+
help='set permissions to prevent writing to the directory')
15+
delete_parser = subparsers.add_parser('delete', help='remove a directory')
16+
delete_parser.add_argument('dirname', action='store', help='the directory to remove')
17+
delete_parser.add_argument('--recursive', '-r', default=False, action='store_true',
18+
help='remove the contents of the directory, too')
19+
print(parser.parse_args())
20+
21+
# python argparse_subparsers.py -h
22+
# python argparse_subparsers.py list -h

module_argparse/argparse_uses_parent.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
parser.add_argument('--local-arg', action='store_true', default=False)
66
print(parser.parse_args())
77

8+
# python argparse_uses_parent.py -h
9+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import argparse
2+
import argparse_parent_with_group
3+
4+
parser = argparse.ArgumentParser(parents=[argparse_parent_with_group.parser])
5+
parser.add_argument('--local-arg', action='store_true', default=False)
6+
7+
print(parser.parse_args())
8+
9+
# python argparse_uses_parent_with_group.py -h
10+

0 commit comments

Comments
 (0)