-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusage.txt
173 lines (121 loc) · 7 KB
/
usage.txt
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
USAGE (clap.py)
Contents:
1. Available methods
1.1 Method "add_avalue"
1.2 Method "add_predef"
1.3 Method "add_switch"
2. Usage example
1. Available methods
1.1 Method "add_avalue"
Add an argument that expects a single user-defined value.
p.add_avalue(arg_short, arg_long, arg_help, arg_dest,
arg_default, arg_required)
Parameters:
arg_short = Short form of the command-line argument.
Expects either a string (e.g. '-i') or
None.
arg_long = Long form of the command-line argument.
Expects a string (e.g. '--input-file').
arg_help = Description for the argument. Expects a
string (e.g. "source file where to read the
data from").
arg_dest = Name of the variable inside the argument
parser the passed value will be stored in,
e.g. 'input_file').
arg_default = Default value. Can be a string, integer or
None.
arg_required = Boolean value if the command-line argument
whether or not is required.
1.2 Method "add_predef"
Add an argument that expects a certain predefined value.
p.add_predef(arg_short, arg_long, arg_help, arg_dest,
arg_choices, arg_required)
Parameters:
arg_short = Short form of the command-line argument.
Expects either a string (e.g. '-a') or
None.
arg_long = Long form of the command-line argument.
Expects a string (e.g. '--action').
arg_help = Description for the argument. Expects a
string (e.g. "action to perform on the
input file").
arg_dest = Name of the variable inside the argument
parser the passed value will be stored in,
e.g. 'input_file').
arg_choices = List of predefined values to choose from,
e.g. '["print", "read", "write"]'.
arg_required = Boolean value if the command-line argument
whether or not is required.
1.3 Method "add_switch"
Add an argument that does not expect anything, but returns a
boolean value.
p.add_switch(arg_short, arg_long, arg_help, arg_dest,
arg_store, arg_required)
Parameters:
arg_short = Short form of the command-line argument.
Expects either a string (e.g. '-a') or
None.
arg_long = Long form of the command-line argument.
Expects a string (e.g. '--action').
arg_help = Description for the argument. Expects a
string (e.g. "action to perform on the
input file").
arg_dest = Name of the variable inside the argument
parser the passed value will be stored in,
e.g. 'input_file').
arg_store = Boolean value that will be returned when the
argument is given. Either True or False.
arg_required = Boolean value if the command-line argument
whether or not is required.
2. Usage example
Below is a simple usage example for the Clap module.
import clap
import sys
p = clap.Parser()
p.set_description("Read a file and write its content into " \
"another.")
p.set_epilog("Further information and usage examples can be " \
"found inside the documentation file for this " \
"script.")
# Required arguments
p.add_avalue("-i", "--input-file", "source file where to read " \
"the data from", "input_file", None, True)
p.add_avalue("-o", "--output-file", "destination file where to " \
"write data into", "output_file", None, True)
p.add_predef("-t", "--file-type", "type of the file", "file_type",
["binary", "raw"], True)
# Optional arguments
p.add_avalue("-b", "--buffer-size", "buffer size in bytes",
"buffer_size", 4096, False)
p.add_switch("-s", "--simulate", "do not create the output file",
"simulate", True, False)
if len(sys.argv) == 1:
p.error("At least one required argument is missing.")
elif ("-h" in sys.argv) or ("--help" in sys.argv):
p.print_help()
sys.exit(0)
args = p.parse_args()
Now, when running the script with the argument '-h' (or '--help'), it will
print something like this:
usage: foobar.py -i INPUT_FILE -o OUTPUT_FILE [-b BUFFER_SIZE] [-s]
Read a file and write its content into another.
required arguments:
-i INPUT_FILE, --input-file INPUT_FILE
source file where to read the data from
-o OUTPUT_FILE, --output-file OUTPUT_FILE
destination file where to write data into
-t {binary,raw}, --file-type {binary,raw}
type of the file
optional arguments:
-b BUFFER_SIZE, --buffer-size BUFFER_SIZE
buffer size in bytes (default is 4096)
-s, --simulate do not create the output file
Further information and usage examples can be found inside the
documentation file for this script.
Inside the code you can use the given command-line arguments as follows,
e.g. to return each of them:
print("Input file path: %s" % args.input_file)
print("Output file path: %s" % args.output_file)
print("File type: %s" % args.file_type)
print("Buffer size: %s" % str(args.buffer_size))
print("Simulate: %s" % str(args.simulate))