-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from cofyc/sub-cmd
add sub command test and example
- Loading branch information
Showing
12 changed files
with
177 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
build/ | ||
CMakeCache.txt | ||
CMakeFiles/ | ||
cmake_install.cmake | ||
tags | ||
test_argparse | ||
*.[ao] | ||
*.dylib | ||
*.so | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
basic | ||
subcommands |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
all:: test | ||
|
||
OBJS += $(patsubst %.c,%,$(wildcard *.c)) | ||
|
||
${OBJS}: %: %.c ../argparse.c | ||
$(CC) -I../ -o $@ ${DEP_LIBS} $^ | ||
|
||
test: ${OBJS} | ||
prove *.sh | ||
.PHONY: test | ||
|
||
clean: | ||
rm -f ${OBJS} | ||
find . -name '.o' -exec rm \; | ||
.PHONY: clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/bin/bash | ||
|
||
. $(dirname ${BASH_SOURCE[0]})/tap-functions | ||
plan_no_plan | ||
|
||
is "$(./basic -f --path=/path/to/file a 2>&1)" 'force: 1 | ||
path: /path/to/file | ||
argc: 1 | ||
argv[0]: a' | ||
|
||
is "$(./basic -f -f --force --no-force 2>&1)" 'force: 2' | ||
|
||
is "$(./basic -i 2>&1)" 'error: option `-i` requires a value' | ||
|
||
is "$(./basic -i 2 2>&1)" 'int_num: 2' | ||
|
||
is "$(./basic -i2 2>&1)" 'int_num: 2' | ||
|
||
is "$(./basic -ia 2>&1)" 'error: option `-i` expects an integer value' | ||
|
||
is "$(./basic -i 0xFFFFFFFFFFFFFFFFF 2>&1)" \ | ||
'error: option `-i` numerical result out of range' | ||
|
||
is "$(./basic -s 2.4 2>&1)" 'flt_num: 2.4' | ||
|
||
is "$(./basic -s2.4 2>&1)" 'flt_num: 2.4' | ||
|
||
is "$(./basic -sa 2>&1)" 'error: option `-s` expects a numerical value' | ||
|
||
is "$(./basic -s 1e999 2>&1)" \ | ||
'error: option `-s` numerical result out of range' | ||
|
||
is "$(./basic -f -- do -f -h 2>&1)" 'force: 1 | ||
argc: 3 | ||
argv[0]: do | ||
argv[1]: -f | ||
argv[2]: -h' | ||
|
||
is "$(./basic -tf 2>&1)" 'force: 1 | ||
test: 1' | ||
|
||
is "$(./basic --read --write 2>&1)" 'perms: 3' | ||
|
||
help_usage='Usage: basic [options] [[--] args] | ||
or: basic [options] | ||
A brief description of what the program does and how it works. | ||
-h, --help show this help message and exit | ||
Basic options | ||
-f, --force force to do | ||
-t, --test test only | ||
-p, --path=<str> path to read | ||
-i, --int=<int> selected integer | ||
-s, --float=<flt> selected float | ||
Bits options | ||
--read read perm | ||
--write write perm | ||
--exec exec perm | ||
Additional description of the program after the description of the arguments.' | ||
|
||
is "$(./basic -h)" "$help_usage" | ||
|
||
is "$(./basic --help)" "$help_usage" | ||
|
||
is "$(./basic --no-help 2>&1)" 'error: unknown option `--no-help`'$'\n'"$help_usage" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include "argparse.h" | ||
|
||
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) | ||
|
||
static const char *const usages[] = { | ||
"subcommands [options] [cmd] [args]", | ||
NULL, | ||
}; | ||
|
||
struct cmd_struct { | ||
const char *cmd; | ||
int len; | ||
int (*fn) (int, const char **); | ||
const char *help; | ||
}; | ||
|
||
int | ||
cmd_foo(int argc, const char **argv) | ||
{ | ||
printf("executing subcommand foo\n"); | ||
for (int i = 0; i < argc; i++) { | ||
printf("argv[%d]: %s\n", i, *(argv + i)); | ||
} | ||
return 0; | ||
} | ||
|
||
int | ||
cmd_bar(int argc, const char **argv) | ||
{ | ||
printf("executing subcommand bar\n"); | ||
for (int i = 0; i < argc; i++) { | ||
printf("argv[%d]: %s\n", i, *(argv + i)); | ||
} | ||
return 0; | ||
} | ||
|
||
static struct cmd_struct commands[] = { | ||
{"foo", 3, cmd_foo, NULL}, | ||
{"bar", 3, cmd_bar, NULL}, | ||
}; | ||
|
||
int | ||
main(int argc, const char **argv) | ||
{ | ||
struct argparse argparse; | ||
struct argparse_option options[] = { | ||
OPT_HELP(), | ||
OPT_END(), | ||
}; | ||
argparse_init(&argparse, options, usages, 0); | ||
argc = argparse_parse(&argparse, argc, argv); | ||
if (argc < 1) { | ||
argparse_usage(&argparse); | ||
return -1; | ||
} | ||
|
||
/* Try to run command with args provided. */ | ||
struct cmd_struct *cmd = NULL; | ||
for (int i = 0; i < ARRAY_SIZE(commands); i++) { | ||
if (!strcmp(commands[i].cmd, argv[0])) { | ||
cmd = &commands[i]; | ||
} | ||
} | ||
if (cmd) { | ||
return cmd->fn(argc, argv); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
|
||
. $(dirname ${BASH_SOURCE[0]})/tap-functions | ||
plan_no_plan | ||
|
||
is "$(./subcommands foo)" 'executing subcommand foo | ||
argv[0]: foo' |
File renamed without changes.