diff --git a/README.rst b/README.rst index a90e1c0..4097b0a 100644 --- a/README.rst +++ b/README.rst @@ -48,22 +48,21 @@ following command line options/arguments: * An optional float value (``-f`` or ``--float``) * A flag (``-q``) -Run ``duckargs``, and pass all possible options/arguments/values that you want you program -to be able to handle, and ``duckargs`` will generate a working program with all the boilerplate -taken care of: +You can run ``duckargs`` and pass all those options/arguments/flags, and ``duckargs`` will +generate a working program with all the boilerplate taken care of: **Generating Python** .. code:: - $ duckargs somestring -i --int 99 -f --float 7.7 -q + $ duckargs somestring -i --intval 99 -f --floatval 7.7 -q **Output** .. code:: # Generated by duckargs, invoked with the following arguments: - # somestring -i --int 99 -f --float 7.7 -q + # somestring -i --intval 99 -f --floatval 7.7 -q import argparse @@ -72,14 +71,14 @@ taken care of: formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('somestring', help='a string') - parser.add_argument('-i', '--int', default=99, type=int, help='an int value') - parser.add_argument('-f', '--float', default=7.7, type=float, help='a float value') + parser.add_argument('-i', '--intval', default=99, type=int, help='an int value') + parser.add_argument('-f', '--floatval', default=7.7, type=float, help='a float value') parser.add_argument('-q', action='store_true', help='q flag') args = parser.parse_args() print(args.somestring) - print(args.int) - print(args.float) + print(args.intval) + print(args.floatval) print(args.q) if __name__ == "__main__": @@ -89,14 +88,14 @@ taken care of: .. code:: - $ duckargs-c somestring -i --int 99 -f --float 7.7 -q + $ duckargs-c somestring -i --intval 99 -f --floatval 7.7 -q **Output** .. code:: // Generated by duckargs, invoked with the following arguments: - // somestring -i --int 99 -f --float 7.7 -q + // somestring -i --intval 99 -f --floatval 7.7 -q #include #include @@ -104,24 +103,26 @@ taken care of: #include static char *somestring = "somestring"; - static long int int = 99; - static float float = 7.7; + static long int intval = 99; + static float floatval = 7.7; static bool q = false; static struct option long_options[] = { - {"int", required_argument, NULL, 'i'}, - {"float", required_argument, NULL, 'f'}, + {"intval", required_argument, NULL, 'i'}, + {"floatval", required_argument, NULL, 'f'}, {NULL, 0, NULL, 0} }; void print_usage(void) { printf("\n"); - printf("program_name [OPTIONS] somestring\n\n"); - printf("-i --int [int] An int value (default: %ld)\n", int); - printf("-f --float [float] A float value (default: %.2f)\n", float); - printf("-q A flag\n"); + printf("USAGE:\n\n"); + printf("program_name [OPTIONS] somestring\n"); + printf("\nOPTIONS:\n\n"); + printf("-i --intval [int] An int value (default: %ld)\n", int); + printf("-f --floatval [float] A float value (default: %.2f)\n", float); + printf("-q A flag\n"); printf("\n"); } @@ -136,7 +137,7 @@ taken care of: { case 'i': { - int = strtol(optarg, &endptr, 0); + intval = strtol(optarg, &endptr, 0); if (endptr && (*endptr != '\0')) { printf("Option '-i' requires an integer argument\n"); @@ -146,7 +147,7 @@ taken care of: } case 'f': { - float = strtof(optarg, &endptr); + floatval = strtof(optarg, &endptr); if (endptr == optarg) { printf("Option '-f' requires a floating-point argument\n"); @@ -187,9 +188,9 @@ taken care of: return ret; } - printf("somestring: %s\n", somestring); - printf("int: %ld\n", int); - printf("float: %.4f\n", float); + printf("somestring: %s\n", somestring ? somestring : "null"); + printf("intval: %ld\n", intval); + printf("floatval: %.4f\n", floatval); printf("q: %s\n", q ? "true" : "false"); return 0; @@ -204,214 +205,6 @@ Install with pip (python 3x required): pip install duckargs -Examples -======== - -Generating python code -###################### - -To generate python code, run duckargs from the command line via ``duckargs`` or ``duckargs-python``, -followed by whatever arguments/options/flags you want your program to accept, and duckargs will -print the corresponding python code. For example: - -:: - - $ duckargs positional_arg1 positional_arg2 -i --int-val 4 -e 3.3 -f --file FILE -F --otherfile FILE -a -b -c - - -The output of the above command looks like this: - - -.. code:: python - - # Generated by duckargs, invoked with the following arguments: - # positional_arg1 positional_arg2 -i --int-val 4 -e 3.3 -f --file FILE -F --otherfile FILE -a -b -c - - import argparse - - def main(): - parser = argparse.ArgumentParser(description='A command-line program generated by duckargs', - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - - parser.add_argument('positional_arg1', help='a string') - parser.add_argument('positional_arg2', help='a string') - parser.add_argument('-i', '--int-val', default=4, type=int, help='an int value') - parser.add_argument('-e', default=3.3, type=float, help='a float value') - parser.add_argument('-f', '--file', default=None, type=argparse.FileType(), help='a filename') - parser.add_argument('-F', '--otherfile', default=None, type=argparse.FileType(), help='a filename') - parser.add_argument('-a', action='store_true', help='a flag') - parser.add_argument('-b', action='store_true', help='b flag') - parser.add_argument('-c', action='store_true', help='c flag') - args = parser.parse_args() - - print(args.positional_arg1) - print(args.positional_arg2) - print(args.int_val) - print(args.e) - print(args.file) - print(args.otherfile) - print(args.a) - print(args.b) - print(args.c) - - if __name__ == "__main__": - main() - -Generating C code -################# - -For generating C code, the process is the same as for python code, except you should call ``duckargs-c`` -instead of ``duckargs-python``: - -:: - - $ duckargs-c positional_arg1 positional_arg2 -i --int-val 4 -e 3.3 -f --file FILE -F --otherfile FILE -a -b -c - -The output of the above command looks like this: - -.. code:: c - - // Generated by duckargs, invoked with the following arguments: - // positional_arg1 positional_arg2 -i --int-val 4 -e 3.3 -f --file FILE -F --otherfile FILE -a -b -c - - #include - #include - #include - #include - - static char *positional_arg1 = "positional_arg1"; - static char *positional_arg2 = "positional_arg2"; - static long int int_val = 4; - static float e = 3.3; - static char *file = NULL; - static char *otherfile = NULL; - static bool a = false; - static bool b = false; - static bool c = false; - - static struct option long_options[] = - { - {"int-val", required_argument, NULL, 'i'}, - {"file", required_argument, NULL, 'f'}, - {"otherfile", required_argument, NULL, 'F'}, - {NULL, 0, NULL, 0} - }; - - void print_usage(void) - { - printf("\n"); - printf("USAGE:\n\n"); - printf("program_name [OPTIONS] positional_arg1 positional_arg2\n"); - printf("\nOPTIONS:\n\n"); - printf("-i --int-val [int] An int value (default: %ld)\n", int_val); - printf("-e [float] A float value (default: %.2f)\n", e); - printf("-f --file FILE A filename (default: %s)\n", file ? file : "null"); - printf("-F --otherfile FILE A filename (default: %s)\n", otherfile ? otherfile : "null"); - printf("-a A flag\n"); - printf("-b A flag\n"); - printf("-c A flag\n"); - printf("\n"); - } - - int parse_args(int argc, char *argv[]) - { - char *endptr = NULL; - int ch; - - while ((ch = getopt_long(argc, argv, "i:e:f:F:abc", long_options, NULL)) != -1) - { - switch (ch) - { - case 'i': - { - int_val = strtol(optarg, &endptr, 0); - if (endptr && (*endptr != '\0')) - { - printf("Option '-i' requires an integer argument\n"); - return -1; - } - break; - } - case 'e': - { - e = strtof(optarg, &endptr); - if (endptr == optarg) - { - printf("Option '-e' requires a floating-point argument\n"); - return -1; - } - break; - } - case 'f': - { - file = optarg; - break; - } - case 'F': - { - otherfile = optarg; - break; - } - case 'a': - { - a = true; - break; - } - case 'b': - { - b = true; - break; - } - case 'c': - { - c = true; - break; - } - } - } - - if (argc < (optind + 2)) - { - printf("Missing positional arguments\n"); - return -1; - } - - positional_arg1 = argv[optind]; - optind++; - - positional_arg2 = argv[optind]; - - return 0; - } - - int main(int argc, char *argv[]) - { - if (argc < 2) - { - print_usage(); - return -1; - } - - int ret = parse_args(argc, argv); - if (0 != ret) - { - return ret; - } - - printf("positional_arg1: %s\n", positional_arg1 ? positional_arg1 : "null"); - printf("positional_arg2: %s\n", positional_arg2 ? positional_arg2 : "null"); - printf("int_val: %ld\n", int_val); - printf("e: %.4f\n", e); - printf("file: %s\n", file ? file : "null"); - printf("otherfile: %s\n", otherfile ? otherfile : "null"); - printf("a: %s\n", a ? "true" : "false"); - printf("b: %s\n", b ? "true" : "false"); - printf("c: %s\n", c ? "true" : "false"); - - return 0; - } - - Comma-separated choices for option argument ===========================================