diff options
author | Randall Leeds <randall@apache.org> | 2011-10-02 04:11:54 -0700 |
---|---|---|
committer | Randall Leeds <randall@apache.org> | 2011-10-02 04:11:54 -0700 |
commit | a378a6a13981b962039c8cd11e11caf0759965bd (patch) | |
tree | 351c7609ffc3e07aa8f9c9b62bcd2a178fedac65 /src/couchdb/priv/couch_js/util.c | |
parent | e77949221f63a011787118637cb549abfbd8e5e8 (diff) |
improve argument parsing in couchjs
This change makes argument parsing in couchjs more robust:
* Removes the need for couchjs script
* Adds long options
* Makes all the options to couchjs available (fix COUCHDB-893)
Conflicts:
bin/Makefile.am
bin/couchjs.tpl.in
configure.ac
Diffstat (limited to 'src/couchdb/priv/couch_js/util.c')
-rw-r--r-- | src/couchdb/priv/couch_js/util.c | 50 |
1 files changed, 38 insertions, 12 deletions
diff --git a/src/couchdb/priv/couch_js/util.c b/src/couchdb/priv/couch_js/util.c index 070d7172..ab1295f8 100644 --- a/src/couchdb/priv/couch_js/util.c +++ b/src/couchdb/priv/couch_js/util.c @@ -15,6 +15,7 @@ #include <jsapi.h> +#include "help.h" #include "util.h" #include "utf8.h" @@ -79,31 +80,56 @@ couch_parse_args(int argc, const char* argv[]) args->stack_size = 8L * 1024L; while(i < argc) { - if(strcmp("--http", argv[i]) == 0) { + if(strcmp("-h", argv[i]) == 0 || + strcmp("--help", argv[i]) == 0) { + DISPLAY_USAGE; + exit(0); + } else if(strcmp("-V", argv[i]) == 0 || + strcmp("--version", argv[i]) == 0) { + DISPLAY_VERSION; + exit(0); + } else if(strcmp("-H", argv[i]) == 0 || + strcmp("--http", argv[i]) == 0) { args->use_http = 1; - } else if(strcmp("--stack-size", argv[i]) == 0) { - args->stack_size = atoi(argv[i+1]); + } else if(strncmp("--stack-size", argv[i], 12) == 0) { + if(argv[i][12] == '\0') { + args->stack_size = atoi(argv[++i]); + } else if(argv[i][12] == '=') { + args->stack_size = atoi(argv[i]+13); + } else { + DISPLAY_USAGE; + exit(2); + } + if(args->stack_size <= 0) { fprintf(stderr, "Invalid stack size.\n"); exit(2); } + } else if(strcmp("--", argv[i]) == 0) { + i++; + break; } else { - args->script = slurp_file(args->script, argv[i]); - if(args->script_name == NULL) { - if(strcmp(argv[i], "-") == 0) { - args->script_name = "<stdin>"; - } else { - args->script_name = argv[i]; - } + break; + } + i++; + } + + while(i < argc) { + args->script = slurp_file(args->script, argv[i]); + if(args->script_name == NULL) { + if(strcmp(argv[i], "-") == 0) { + args->script_name = "<stdin>"; } else { - args->script_name = "<multiple_files>"; + args->script_name = argv[i]; } + } else { + args->script_name = "<multiple_files>"; } i++; } if(args->script_name == NULL || args->script == NULL) { - fprintf(stderr, "No script provided.\n"); + DISPLAY_USAGE; exit(3); } |