-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargparse_.py
More file actions
60 lines (53 loc) · 2.27 KB
/
argparse_.py
File metadata and controls
60 lines (53 loc) · 2.27 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
""" Action Classes and Parser wrappers for the argparse module.
"""
import argparse
import logging
class LogLevel(argparse.Action):
""" Action class for argparse to set/change the log level.
"""
def __call__(self, parser, namespace, values=None, option_string=None):
if isinstance(option_string, basestring):
# get increment
if '-q' in option_string:
inc = 10
elif '-v' in option_string:
inc = -10
else:
return
# Get level
try:
level = getattr(namespace, self.dest) + inc
except TypeError:
# Use default base level
try:
level = getattr(logging, self.default) + inc
except TypeError:
level = logging.getLogger().level + inc
# Set level
setattr(namespace, self.dest, level if level else 1)
class GetPass(argparse.Action):
""" Action class for argparse to read a password from the commandline.
"""
def __call__(self, parser, namespace, values=None, option_string=None):
import getpass
setattr(namespace, self.dest, getpass.getpass())
class ArgumentParser(argparse.ArgumentParser):
""" Class that adds log_level parsing by default.
"""
def __init__(self, *args, **kwargs):
super(ArgumentParser, self).__init__(*args, **kwargs)
self.add_argument('-v', '--verbose', nargs=0, dest='log_level',
action=LogLevel, help="Louder execution [repeatable].")
self.add_argument('-q', '--quiet', nargs=0, dest='log_level',
action=LogLevel, help="Quieter execution [repeatable].")
class AuthArgumentParser(ArgumentParser):
""" Class that adds username and password parsing by default.
"""
def __init__(self, *args, **kwargs):
super(AuthArgumentParser, self).__init__(*args, **kwargs)
self.add_argument('-u', '--user', '--username',
help="Username for authentication.")
self.add_argument('-p', '--passwd', '--password',
help="Password for authentication.")
self.add_argument('-a', '--askpass', nargs=0, dest='passwd',
action=GetPass, help="Prompt for password.")