Skip to content

Commit 7534a83

Browse files
committed
README: more updates, delete uneccessary sections
1 parent 91282ff commit 7534a83

File tree

1 file changed

+25
-232
lines changed

1 file changed

+25
-232
lines changed

README.rst

Lines changed: 25 additions & 232 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,21 @@ following command line options/arguments:
4848
* An optional float value (``-f`` or ``--float``)
4949
* A flag (``-q``)
5050

51-
Run ``duckargs``, and pass all possible options/arguments/values that you want you program
52-
to be able to handle, and ``duckargs`` will generate a working program with all the boilerplate
53-
taken care of:
51+
You can run ``duckargs`` and pass all those options/arguments/flags, and ``duckargs`` will
52+
generate a working program with all the boilerplate taken care of:
5453

5554
**Generating Python**
5655

5756
.. code::
5857
59-
$ duckargs somestring -i --int 99 -f --float 7.7 -q
58+
$ duckargs somestring -i --intval 99 -f --floatval 7.7 -q
6059
6160
**Output**
6261

6362
.. code::
6463
6564
# Generated by duckargs, invoked with the following arguments:
66-
# somestring -i --int 99 -f --float 7.7 -q
65+
# somestring -i --intval 99 -f --floatval 7.7 -q
6766
6867
import argparse
6968
@@ -72,14 +71,14 @@ taken care of:
7271
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
7372
7473
parser.add_argument('somestring', help='a string')
75-
parser.add_argument('-i', '--int', default=99, type=int, help='an int value')
76-
parser.add_argument('-f', '--float', default=7.7, type=float, help='a float value')
74+
parser.add_argument('-i', '--intval', default=99, type=int, help='an int value')
75+
parser.add_argument('-f', '--floatval', default=7.7, type=float, help='a float value')
7776
parser.add_argument('-q', action='store_true', help='q flag')
7877
args = parser.parse_args()
7978
8079
print(args.somestring)
81-
print(args.int)
82-
print(args.float)
80+
print(args.intval)
81+
print(args.floatval)
8382
print(args.q)
8483
8584
if __name__ == "__main__":
@@ -89,39 +88,41 @@ taken care of:
8988

9089
.. code::
9190
92-
$ duckargs-c somestring -i --int 99 -f --float 7.7 -q
91+
$ duckargs-c somestring -i --intval 99 -f --floatval 7.7 -q
9392
9493
**Output**
9594

9695
.. code::
9796
9897
// Generated by duckargs, invoked with the following arguments:
99-
// somestring -i --int 99 -f --float 7.7 -q
98+
// somestring -i --intval 99 -f --floatval 7.7 -q
10099
101100
#include <stdbool.h>
102101
#include <getopt.h>
103102
#include <stdlib.h>
104103
#include <stdio.h>
105104
106105
static char *somestring = "somestring";
107-
static long int int = 99;
108-
static float float = 7.7;
106+
static long int intval = 99;
107+
static float floatval = 7.7;
109108
static bool q = false;
110109
111110
static struct option long_options[] =
112111
{
113-
{"int", required_argument, NULL, 'i'},
114-
{"float", required_argument, NULL, 'f'},
112+
{"intval", required_argument, NULL, 'i'},
113+
{"floatval", required_argument, NULL, 'f'},
115114
{NULL, 0, NULL, 0}
116115
};
117116
118117
void print_usage(void)
119118
{
120119
printf("\n");
121-
printf("program_name [OPTIONS] somestring\n\n");
122-
printf("-i --int [int] An int value (default: %ld)\n", int);
123-
printf("-f --float [float] A float value (default: %.2f)\n", float);
124-
printf("-q A flag\n");
120+
printf("USAGE:\n\n");
121+
printf("program_name [OPTIONS] somestring\n");
122+
printf("\nOPTIONS:\n\n");
123+
printf("-i --intval [int] An int value (default: %ld)\n", int);
124+
printf("-f --floatval [float] A float value (default: %.2f)\n", float);
125+
printf("-q A flag\n");
125126
printf("\n");
126127
}
127128
@@ -136,7 +137,7 @@ taken care of:
136137
{
137138
case 'i':
138139
{
139-
int = strtol(optarg, &endptr, 0);
140+
intval = strtol(optarg, &endptr, 0);
140141
if (endptr && (*endptr != '\0'))
141142
{
142143
printf("Option '-i' requires an integer argument\n");
@@ -146,7 +147,7 @@ taken care of:
146147
}
147148
case 'f':
148149
{
149-
float = strtof(optarg, &endptr);
150+
floatval = strtof(optarg, &endptr);
150151
if (endptr == optarg)
151152
{
152153
printf("Option '-f' requires a floating-point argument\n");
@@ -187,9 +188,9 @@ taken care of:
187188
return ret;
188189
}
189190
190-
printf("somestring: %s\n", somestring);
191-
printf("int: %ld\n", int);
192-
printf("float: %.4f\n", float);
191+
printf("somestring: %s\n", somestring ? somestring : "null");
192+
printf("intval: %ld\n", intval);
193+
printf("floatval: %.4f\n", floatval);
193194
printf("q: %s\n", q ? "true" : "false");
194195
195196
return 0;
@@ -204,214 +205,6 @@ Install with pip (python 3x required):
204205

205206
pip install duckargs
206207

207-
Examples
208-
========
209-
210-
Generating python code
211-
######################
212-
213-
To generate python code, run duckargs from the command line via ``duckargs`` or ``duckargs-python``,
214-
followed by whatever arguments/options/flags you want your program to accept, and duckargs will
215-
print the corresponding python code. For example:
216-
217-
::
218-
219-
$ duckargs positional_arg1 positional_arg2 -i --int-val 4 -e 3.3 -f --file FILE -F --otherfile FILE -a -b -c
220-
221-
222-
The output of the above command looks like this:
223-
224-
225-
.. code:: python
226-
227-
# Generated by duckargs, invoked with the following arguments:
228-
# positional_arg1 positional_arg2 -i --int-val 4 -e 3.3 -f --file FILE -F --otherfile FILE -a -b -c
229-
230-
import argparse
231-
232-
def main():
233-
parser = argparse.ArgumentParser(description='A command-line program generated by duckargs',
234-
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
235-
236-
parser.add_argument('positional_arg1', help='a string')
237-
parser.add_argument('positional_arg2', help='a string')
238-
parser.add_argument('-i', '--int-val', default=4, type=int, help='an int value')
239-
parser.add_argument('-e', default=3.3, type=float, help='a float value')
240-
parser.add_argument('-f', '--file', default=None, type=argparse.FileType(), help='a filename')
241-
parser.add_argument('-F', '--otherfile', default=None, type=argparse.FileType(), help='a filename')
242-
parser.add_argument('-a', action='store_true', help='a flag')
243-
parser.add_argument('-b', action='store_true', help='b flag')
244-
parser.add_argument('-c', action='store_true', help='c flag')
245-
args = parser.parse_args()
246-
247-
print(args.positional_arg1)
248-
print(args.positional_arg2)
249-
print(args.int_val)
250-
print(args.e)
251-
print(args.file)
252-
print(args.otherfile)
253-
print(args.a)
254-
print(args.b)
255-
print(args.c)
256-
257-
if __name__ == "__main__":
258-
main()
259-
260-
Generating C code
261-
#################
262-
263-
For generating C code, the process is the same as for python code, except you should call ``duckargs-c``
264-
instead of ``duckargs-python``:
265-
266-
::
267-
268-
$ duckargs-c positional_arg1 positional_arg2 -i --int-val 4 -e 3.3 -f --file FILE -F --otherfile FILE -a -b -c
269-
270-
The output of the above command looks like this:
271-
272-
.. code:: c
273-
274-
// Generated by duckargs, invoked with the following arguments:
275-
// positional_arg1 positional_arg2 -i --int-val 4 -e 3.3 -f --file FILE -F --otherfile FILE -a -b -c
276-
277-
#include <stdbool.h>
278-
#include <getopt.h>
279-
#include <stdlib.h>
280-
#include <stdio.h>
281-
282-
static char *positional_arg1 = "positional_arg1";
283-
static char *positional_arg2 = "positional_arg2";
284-
static long int int_val = 4;
285-
static float e = 3.3;
286-
static char *file = NULL;
287-
static char *otherfile = NULL;
288-
static bool a = false;
289-
static bool b = false;
290-
static bool c = false;
291-
292-
static struct option long_options[] =
293-
{
294-
{"int-val", required_argument, NULL, 'i'},
295-
{"file", required_argument, NULL, 'f'},
296-
{"otherfile", required_argument, NULL, 'F'},
297-
{NULL, 0, NULL, 0}
298-
};
299-
300-
void print_usage(void)
301-
{
302-
printf("\n");
303-
printf("USAGE:\n\n");
304-
printf("program_name [OPTIONS] positional_arg1 positional_arg2\n");
305-
printf("\nOPTIONS:\n\n");
306-
printf("-i --int-val [int] An int value (default: %ld)\n", int_val);
307-
printf("-e [float] A float value (default: %.2f)\n", e);
308-
printf("-f --file FILE A filename (default: %s)\n", file ? file : "null");
309-
printf("-F --otherfile FILE A filename (default: %s)\n", otherfile ? otherfile : "null");
310-
printf("-a A flag\n");
311-
printf("-b A flag\n");
312-
printf("-c A flag\n");
313-
printf("\n");
314-
}
315-
316-
int parse_args(int argc, char *argv[])
317-
{
318-
char *endptr = NULL;
319-
int ch;
320-
321-
while ((ch = getopt_long(argc, argv, "i:e:f:F:abc", long_options, NULL)) != -1)
322-
{
323-
switch (ch)
324-
{
325-
case 'i':
326-
{
327-
int_val = strtol(optarg, &endptr, 0);
328-
if (endptr && (*endptr != '\0'))
329-
{
330-
printf("Option '-i' requires an integer argument\n");
331-
return -1;
332-
}
333-
break;
334-
}
335-
case 'e':
336-
{
337-
e = strtof(optarg, &endptr);
338-
if (endptr == optarg)
339-
{
340-
printf("Option '-e' requires a floating-point argument\n");
341-
return -1;
342-
}
343-
break;
344-
}
345-
case 'f':
346-
{
347-
file = optarg;
348-
break;
349-
}
350-
case 'F':
351-
{
352-
otherfile = optarg;
353-
break;
354-
}
355-
case 'a':
356-
{
357-
a = true;
358-
break;
359-
}
360-
case 'b':
361-
{
362-
b = true;
363-
break;
364-
}
365-
case 'c':
366-
{
367-
c = true;
368-
break;
369-
}
370-
}
371-
}
372-
373-
if (argc < (optind + 2))
374-
{
375-
printf("Missing positional arguments\n");
376-
return -1;
377-
}
378-
379-
positional_arg1 = argv[optind];
380-
optind++;
381-
382-
positional_arg2 = argv[optind];
383-
384-
return 0;
385-
}
386-
387-
int main(int argc, char *argv[])
388-
{
389-
if (argc < 2)
390-
{
391-
print_usage();
392-
return -1;
393-
}
394-
395-
int ret = parse_args(argc, argv);
396-
if (0 != ret)
397-
{
398-
return ret;
399-
}
400-
401-
printf("positional_arg1: %s\n", positional_arg1 ? positional_arg1 : "null");
402-
printf("positional_arg2: %s\n", positional_arg2 ? positional_arg2 : "null");
403-
printf("int_val: %ld\n", int_val);
404-
printf("e: %.4f\n", e);
405-
printf("file: %s\n", file ? file : "null");
406-
printf("otherfile: %s\n", otherfile ? otherfile : "null");
407-
printf("a: %s\n", a ? "true" : "false");
408-
printf("b: %s\n", b ? "true" : "false");
409-
printf("c: %s\n", c ? "true" : "false");
410-
411-
return 0;
412-
}
413-
414-
415208
Comma-separated choices for option argument
416209
===========================================
417210

0 commit comments

Comments
 (0)