initial commit
This commit is contained in:
commit
243cdf9b7a
20
LICENSE
Normal file
20
LICENSE
Normal file
@ -0,0 +1,20 @@
|
||||
MIT License
|
||||
Copyright 2019 Bruce Hill <bruce@bruce-hill.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
35
Makefile
Normal file
35
Makefile
Normal file
@ -0,0 +1,35 @@
|
||||
PREFIX=
|
||||
CC=cc
|
||||
CFLAGS=-O3 -std=gnu99
|
||||
|
||||
all: arg
|
||||
|
||||
clean:
|
||||
rm arg
|
||||
|
||||
arg: arg.c
|
||||
$(CC) arg.c $(CFLAGS) -o arg
|
||||
|
||||
install: arg
|
||||
@prefix="$(PREFIX)"; \
|
||||
if [[ ! $$prefix ]]; then \
|
||||
read -p $$'\033[1mWhere do you want to install? (default: /usr/local) \033[0m' prefix; \
|
||||
fi; \
|
||||
if [[ ! $$prefix ]]; then \
|
||||
prefix="/usr/local"; \
|
||||
fi; \
|
||||
mkdir -pv $$prefix/bin $$prefix/share/man/man1 \
|
||||
&& cp -v arg $$prefix/bin/ \
|
||||
&& cp -v arg.1 $$prefix/share/man/man1/
|
||||
|
||||
uninstall:
|
||||
@prefix="$(PREFIX)"; \
|
||||
if [[ ! $$prefix ]]; then \
|
||||
read -p $$'\033[1mWhere do you want to uninstall from? (default: /usr/local) \033[0m' prefix; \
|
||||
fi; \
|
||||
if [[ ! $$prefix ]]; then \
|
||||
prefix="/usr/local"; \
|
||||
fi; \
|
||||
echo "Deleting..."; \
|
||||
rm -rvf $$prefix/bin/arg $$prefix/share/man/man1/arg.1
|
||||
|
50
arg.c
Normal file
50
arg.c
Normal file
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* arg - A simple C program by Bruce Hill for parsing command line arguments
|
||||
* Licensed under the MIT license (see LICENSE for details).
|
||||
*
|
||||
* Example: arg --foo a b --foo=blah --baz -xyz
|
||||
* - Prints "blah" and exits with success
|
||||
* Example: arg --nope a b --foo=blah --baz -xyz
|
||||
* - Prints nothing and exits with failure
|
||||
* Example: arg -y a b --foo=blah --baz -xyz
|
||||
* - Prints nothing and exits with success
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char *flag = argv[1];
|
||||
if (!flag || flag[0] != '-') {
|
||||
fprintf(stderr, "Usage: arg [-<f> | --<flag>] ...\n");
|
||||
return 1;
|
||||
}
|
||||
size_t n = strlen(flag);
|
||||
// Look for: --flag=val, --flag val, --flag, -f=val, -f val, or -f
|
||||
for (int i = 2; i < argc; i++) {
|
||||
char *arg = argv[i];
|
||||
if (strncmp(arg, flag, n) != 0) continue;
|
||||
if (arg[n] == '\0') { // --flag
|
||||
if (argv[i+1] && argv[i+1][0] != '-')
|
||||
puts(argv[i+1]); // value of flag, if any
|
||||
return 0;
|
||||
} else if (arg[n] == '=') { // --flag=...
|
||||
puts(&arg[n+1]);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
// If flag is single-character, e.g. -f, look for it among other single
|
||||
// character flags like -xfy, in which case the next argument will not
|
||||
// be considered its value.
|
||||
if (n == 2) {
|
||||
for (int i = 2; i < argc; i++) {
|
||||
char *arg = argv[i];
|
||||
if (arg[0] != '-' || arg[1] == '-')
|
||||
continue; // skip foo and --foo
|
||||
for (char *c = &arg[1]; *c; c++) {
|
||||
if (*c == flag[1]) return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
Loading…
Reference in New Issue
Block a user