cofyc / argparse Goto Github PK
View Code? Open in Web Editor NEWCommand-line arguments parsing library.
License: MIT License
Command-line arguments parsing library.
License: MIT License
Hi,
does your library support subcomands such as used in GIT or Argparse?
e.g.
git add *
git --help clone
etc.
Thank you
And what are the flags for argparse_init
? The example only shows 0
.
Any opinions on including a description and epilog? Since this is static library, its easy enough to just edit the source files, but it seems like it would be a commonly-used feature.
Since adding args to argparse_init()
breaks backwards-compatibilty, would the best way to approach this be something line:
int argparse_describe(struct argparse *self, const char *description, const char *epilog)
called after argparse_init()
to set these in the new struct argparse
fields?
When you assign this->output
to argv
, and then later on assign a null
value to this->output[x]
, it also updates argv
to have a null pointer. This is unexpected and argv
shouldn't be modified.
###### Unit Test #####
ok 1
ok 2
ok 3
ok 4
ok 5
not ok 6
# Failed test in ./tap-functions:is() at line 285.
# got: 'error: option `-i` Invalid argument'
# expected: 'error: option `-i` expects an integer value'
not ok 7
# Failed test in ./tap-functions:is() at line 285.
# got: 'error: option `-i` Result too large'
# expected: 'error: option `-i` Numerical result out of range'
ok 8
ok 9
ok 10
not ok 11
# Failed test in ./tap-functions:is() at line 285.
# got: 'error: option `-s` Result too large'
# expected: 'error: option `-s` Numerical result out of range'
ok 12
ok 13
ok 14
ok 15
ok 16
ok 17
1..17
# Looks like you failed 3 tests of 17.
make: *** [test] Error 3
$ test --help
(works as expected)
$ test
UPDATE: It was my fault, sorry for disruption!
Converting the argparse_option::value to a union, containing int, float, etc would save a lot of ugly type casts argparse_getvalue().
I think required arguments would be a great addition. As far as I know, they are not implemented.
Is there a port for this library on vcpkg?
If I do a search I only come up with https://github.com/p-ranav/argparse
比如:OPT_BOOLEAN('t', "test", &test, "test only", NULL, 0, 0),
我想命令行参数中-t带与不带参数均可正常解析能实现吗
This project looks interesting, I look forward to using it in some projects. Thanks for your work.
When exiting due to a parsing error, argparse exits with status 0, but it should probably return EXIT_FAILURE (from stdlib.h) or EX_USAGE (from sysexits.h). If the user passes "-h", then 0 is correct, in my opinion.
Problems:
It would be great if word wrapping long help text lines where supported using for example: http://rosettacode.org/wiki/Word_wrap#C
The feature section on the README talks about positional arguments but no example has been shown and I can't find how it can be done even after reading the code.
Do you want to support REMAINDER somehow?
thanks for making a malloc-free arg parser that has a sane api.
any chance we could add subcommands?
thething subcommand -xyz
So far argparse always requires floating point support because of strtof().
What about some kind of optional implementation?
Is it possible to handle positional arguments? For example, if I want to call my program like foo [options] <filename>
? I don't see anything like that in the source code but I could have easily missed it.
Can it support generating completions for common shells? For python's argparse, we have shtab, can generate shell completion script from a template.
# After installing foo
$ foo --print-completion bash | sudo tee /usr/share/bash-completion/completions/foo
# completion can work
$ foo -<TAB>
install()
in cmake$ cmake -Bbuild -DCMAKE_INSTALL_PREFIX=/usr
$ cmake --build build
$ cmake --install build
$ ls /usr/share/bash-completion/completions/foo
/usr/share/bash-completion/completions/foo
# completion can work
$ foo -<TAB>
I thinks solution 2 will be better?
Now, if I was actually making a compiler, I probably would use a different argument parsing library. How would we deal with multiple options?
If we ran:
./a.out -Ooption1 -Ooption2
I would expect to get something along the lines of {"option1", "option2"}
. Is it possible to do this, or do I have to implement this myself?
I'm planning on using this project in my GNU Moreutils vipe
rewrite, I was curious what version management system this project uses. You have some git tags that seem to be Semantic Versioned but there have been quite a few bug fixes since then and no tagged version patches.
` $ make test
cc -o test_argparse.o -c -Wall -Wextra -fPIC -O3 -g -ggdb test_argparse.c
test_argparse.c: In function ‘main’:
test_argparse.c:28:9: warning: missing initializer for field ‘callback’ of ‘struct argparse_option’ [-Wmissing-field-initializers]
OPT_BOOLEAN('f', "force", &force, "force to do"),
^
In file included from test_argparse.c:4:0:
argparse.h:80:24: note: ‘callback’ declared here
argparse_callback *callback;
^
test_argparse.c:29:9: warning: missing initializer for field ‘callback’ of ‘struct argparse_option’ [-Wmissing-field-initializers]
OPT_BOOLEAN('t', "test", &test, "test only"),
^
In file included from test_argparse.c:4:0:
argparse.h:80:24: note: ‘callback’ declared here
argparse_callback *callback;
^
test_argparse.c:30:9: warning: missing initializer for field ‘callback’ of ‘struct argparse_option’ [-Wmissing-field-initializers]
OPT_STRING('p', "path", &path, "path to read"),
^
In file included from test_argparse.c:4:0:
argparse.h:80:24: note: ‘callback’ declared here
argparse_callback *callback;
^
test_argparse.c:31:9: warning: missing initializer for field ‘callback’ of ‘struct argparse_option’ [-Wmissing-field-initializers]
OPT_INTEGER('i', "int", &int_num, "selected integer"),
^
In file included from test_argparse.c:4:0:
argparse.h:80:24: note: ‘callback’ declared here
argparse_callback *callback;
^
test_argparse.c:32:9: warning: missing initializer for field ‘callback’ of ‘struct argparse_option’ [-Wmissing-field-initializers]
OPT_FLOAT('s', "float", &flt_num, "selected float"),
^
In file included from test_argparse.c:4:0:
argparse.h:80:24: note: ‘callback’ declared here
argparse_callback *callback;
^
test_argparse.c:35:9: warning: missing initializer for field ‘flags’ of ‘struct argparse_option’ [-Wmissing-field-initializers]
OPT_BIT(0, "write", &perms, "write perm", NULL, PERM_WRITE),
^
In file included from test_argparse.c:4:0:
argparse.h:82:9: note: ‘flags’ declared here
int flags;
^
test_argparse.c:36:9: warning: missing initializer for field ‘flags’ of ‘struct argparse_option’ [-Wmissing-field-initializers]
OPT_BIT(0, "exec", &perms, "exec perm", NULL, PERM_EXEC),
^
In file included from test_argparse.c:4:0:
argparse.h:82:9: note: ‘flags’ declared here
int flags;
^
cc -Wall -Wextra -fPIC -O3 -g -ggdb -o test_argparse argparse.o test_argparse.o
ok 1
ok 2
ok 3
ok 4
ok 5
ok 6
ok 7
ok 8
ok 9
ok 10
ok 11
ok 12
ok 13
ok 14
ok 15
1..15`
I'm doing some C code experiments using this very nice argparse implementation. I wrote a small script that clones repositories so that it's easy to install third party code such as this. For that reason it'd be preferable for me if there was a tag. (Since you can clone tags/branches directly, something you can't do for commit hashes.) Right now it seems the code works quite fine, so maybe a 1.0.0 release is in order?
Hi Cofyc. Some time ago I forked argparse for a personal need.
I added the capability to call a function associated with an argument and a new type ARGPARSE_OPT_ARRAY that works and allocate char ** .
My fork can be found here:
https://github.com/FraMecca/libargparse
and this is the fork in action:
http://francescomecca.eu:3000/pesceWanda/kpd/src/development/src/main.c
If you want and if it is needed, we can work to merge my code to your original work
Let me know if interested.
Francesco
Hi,
Why the memmove at the end of argparse ?
It destroys the original argv array
Thanks for a great library!
I was just wondering, how could I implement default values easily?
That is, if --myoption
argument is not specified on the command line, then, say, the corresponding float myoption
in C code gets the value (for example) 5.0 ...
I have seen in the code https://github.com/cofyc/argparse/blob/master/argparse.h, the last three arguments to the macros are:
* `callback`:
* function is called when corresponding argument is parsed.
*
* `data`:
* associated data. Callbacks can use it like they want.
*
* `flags`:
* option flags.
So, I guess, I'd have to implement a custom callback for all such options, and then assign data in the callback, in case the command line argument was not found on the command line. But then, I'm not sure how could I check in the callback, whether the command line argument was present or not?
Is there an example that shows such usage (with default values for arguments, if they are not set on the command line)?
There is a callback which returns an int value. How is this handled? Does it override the value assignment for integers?
I assume the intptr_t
field in options is used to pass a value to it, but that value is limited to 32-bits on x86 but 64-bits on amd64. Did you intend to make this architecture-dependent? Why not void*?
Hi! Could a flag be used with parameter and without parameter simultaneously?
For example smth like:
#define INT_DEFAULT_VALUE 10
OPT_BOOLEAN('f', "force", &force, "force to do", NULL, 0, 0),
OPT_INTEGER('i', "int", &int_num, "selected integer", NULL, 0, 0), //some way to transfer INT_DEFAULT_VALUE inside parser
my cmd -i 15 -f
force: 1
int_num: 15
Now it outputs next string:
my cmd -i -f
[int_num: 15](error: option
-i
expects an integer value)
But could the parameter be used with a default value like this:
my cmd -i -f
force: 1
int_num: 10
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.