You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@couchdb.apache.org by Paul Davis <pa...@gmail.com> on 2011/10/02 20:00:13 UTC

Re: [3/3] git commit: improve argument parsing in couchjs

Looks like I should add the branch name to these emails and not number
them as part of the same push. I was quite confused why I had three
emails about the same commit till I realize Randall pushed the same
update onto three branches.

On Sun, Oct 2, 2011 at 6:14 AM,  <ra...@apache.org> wrote:
> 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)
>
>
> Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo
> Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/b09c2694
> Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/b09c2694
> Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/b09c2694
>
> Branch: refs/heads/master
> Commit: b09c2694b36e6f91d19e9501ad3d2904c537a2b3
> Parents: 25673c2
> Author: Randall Leeds <ra...@apache.org>
> Authored: Sun Oct 2 04:11:45 2011 -0700
> Committer: Randall Leeds <ra...@apache.org>
> Committed: Sun Oct 2 04:11:45 2011 -0700
>
> ----------------------------------------------------------------------
>  bin/Makefile.am                  |   37 +++----------
>  bin/couchjs.tpl.in               |   95 ---------------------------------
>  configure.ac                     |    6 +-
>  src/couchdb/priv/Makefile.am     |   14 +++++
>  src/couchdb/priv/couch_js/help.h |   82 ++++++++++++++++++++++++++++
>  src/couchdb/priv/couch_js/util.c |   50 +++++++++++++----
>  src/couchdb/priv/couch_js/util.h |    1 -
>  7 files changed, 144 insertions(+), 141 deletions(-)
> ----------------------------------------------------------------------
>
>
> http://git-wip-us.apache.org/repos/asf/couchdb/blob/b09c2694/bin/Makefile.am
> ----------------------------------------------------------------------
> diff --git a/bin/Makefile.am b/bin/Makefile.am
> index 5737e0a..320c8a7 100644
> --- a/bin/Makefile.am
> +++ b/bin/Makefile.am
> @@ -13,13 +13,13 @@
>  if WINDOWS
>  bin_SCRIPTS = couchdb.bat
>  else
> -bin_SCRIPTS = couchdb couchjs couch-config
> +bin_SCRIPTS = couchdb couch-config
>  endif
>
>  noinst_SCRIPTS = couchjs_dev couch-config_dev
>
>  if HELP2MAN
> -dist_man1_MANS = couchdb.1 couchjs.1
> +dist_man1_MANS = couchdb.1
>  endif
>
>  CLEANFILES = $(bin_SCRIPTS) $(dist_man1_MANS) $(noinst_SCRIPTS)
> @@ -48,27 +48,12 @@ couchdb: couchdb.tpl
>        $@ < $<
>        chmod +x $@
>
> -couchjs: couchjs.tpl
> -       sed -e "s|%locallibbindir%|@locallibbindir@|g" \
> -           -e "s|%bug_uri%|@bug_uri@|g" \
> -           -e "s|%package_author_address%|@package_author_address@|g" \
> -           -e "s|%package_author_name%|@package_author_name@|g" \
> -           -e "s|%package_name%|@package_name@|g" \
> -           -e "s|%version%|@version@|g" \
> -           -e "s|%couchjs_command_name%|$(couchjs_command_name)|g" > \
> -       $@ < $<
> -       chmod +x $@
> +install-exec-hook:
> +       $(LN_S) -f "$(locallibbindir)/$(couchjs_command_name)" \
> +               "$(DESTDIR)$(bindir)/$(couchjs_command_name)"
>
> -couchjs_dev: couchjs.tpl
> -       sed -e "s|%locallibbindir%|$(abs_top_builddir)/src/couchdb/priv|g" \
> -           -e "s|%bug_uri%|@bug_uri@|g" \
> -           -e "s|%package_author_address%|@package_author_address@|g" \
> -           -e "s|%package_author_name%|@package_author_name@|g" \
> -           -e "s|%package_name%|@package_name@|g" \
> -           -e "s|%version%|@version@|g" \
> -           -e "s|%couchjs_command_name%|$(couchjs_command_name)|g" > \
> -       $@ < $<
> -       chmod +x $@
> +couchjs_dev:
> +       $(LN_S) -f "$(abs_top_builddir)/src/couchdb/priv/couchjs" "$@"
>
>  couchdb.bat: couchdb.bat.tpl
>        sed -e "s|%ICU_CONFIG%|$(ICU_CONFIG)|g" \
> @@ -136,11 +121,3 @@ couchdb.1: couchdb.tpl.in
>            $(HELP2MAN_EXECUTABLE) $(HELP2MAN_OPTION) \
>                --name="Apache CouchDB database server" ./couchdb --output $@; \
>        fi
> -
> -couchjs.1: couchjs.tpl.in
> -       touch $@
> -       if test -x "$(HELP2MAN_EXECUTABLE)"; then \
> -           $(MAKE) -f Makefile couchjs; \
> -           $(HELP2MAN_EXECUTABLE) $(HELP2MAN_OPTION) \
> -               --name="Apache CouchDB JavaScript interpreter" ./couchjs --output $@; \
> -       fi
>
> http://git-wip-us.apache.org/repos/asf/couchdb/blob/b09c2694/bin/couchjs.tpl.in
> ----------------------------------------------------------------------
> diff --git a/bin/couchjs.tpl.in b/bin/couchjs.tpl.in
> deleted file mode 100644
> index 2481dcd..0000000
> --- a/bin/couchjs.tpl.in
> +++ /dev/null
> @@ -1,95 +0,0 @@
> -#! /bin/sh -e
> -
> -# Licensed under the Apache License, Version 2.0 (the "License"); you may not
> -# use this file except in compliance with the License. You may obtain a copy of
> -# the License at
> -#
> -#   http://www.apache.org/licenses/LICENSE-2.0
> -#
> -# Unless required by applicable law or agreed to in writing, software
> -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
> -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
> -# License for the specific language governing permissions and limitations under
> -# the License.
> -
> -SCRIPT_OK=0
> -SCRIPT_ERROR=1
> -
> -DEFAULT_VERSION=170
> -
> -basename=`basename $0`
> -
> -display_version () {
> -    cat << EOF
> -$basename - %package_name% %version%
> -
> -Licensed under the Apache License, Version 2.0 (the "License"); you may not use
> -this file except in compliance with the License. You may obtain a copy of the
> -License at
> -
> -  http://www.apache.org/licenses/LICENSE-2.0
> -
> -Unless required by applicable law or agreed to in writing, software distributed
> -under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
> -CONDITIONS OF ANY KIND, either express or implied. See the License for the
> -specific language governing permissions and limitations under the License.
> -EOF
> -}
> -
> -display_help () {
> -    cat << EOF
> -Usage: $basename [FILE]
> -
> -The $basename command runs the %package_name% JavaScript interpreter.
> -
> -The exit status is 0 for success or 1 for failure.
> -
> -Options:
> -
> -  -h  display a short help message and exit
> -  -V  display version information and exit
> -  -H  install couchjs cURL bindings (only avaiable
> -      if CouchDB was built with cURL available)
> -
> -Report bugs at <%bug_uri%>.
> -EOF
> -}
> -
> -display_error () {
> -    if test -n "$1"; then
> -        echo $1 >&2
> -    fi
> -    echo >&2
> -    echo "Try \`"$basename" -h' for more information." >&2
> -    exit $SCRIPT_ERROR
> -}
> -
> -run_couchjs () {
> -    exec %locallibbindir%/%couchjs_command_name% $@
> -}
> -
> -parse_script_option_list () {
> -    set +e
> -    options=`getopt hVH $@`
> -    if test ! $? -eq 0; then
> -        display_error
> -    fi
> -    set -e
> -    eval set -- $options
> -    while [ $# -gt 0 ]; do
> -        case "$1" in
> -            -h) shift; display_help; exit $SCRIPT_OK;;
> -            -V) shift; display_version; exit $SCRIPT_OK;;
> -            --) shift; break;;
> -            *) break;;
> -        esac
> -    done
> -    script_name=`echo $@ | sed -e 's/.*--[[:blank:]]*//'`
> -    if test -z "$script_name"; then
> -        display_error "You must specify a FILE."
> -    fi
> -    options=`echo $@ | sed -e 's/--//'`
> -    run_couchjs $options
> -}
> -
> -parse_script_option_list $@
>
> http://git-wip-us.apache.org/repos/asf/couchdb/blob/b09c2694/configure.ac
> ----------------------------------------------------------------------
> diff --git a/configure.ac b/configure.ac
> index 049ef19..89d9b10 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -10,7 +10,8 @@ dnl WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
>  dnl License for the specific language governing permissions and limitations
>  dnl under the License.
>
> -AC_INIT([LOCAL_PACKAGE_NAME], [LOCAL_VERSION], [], [LOCAL_PACKAGE_TARNAME])
> +AC_INIT([LOCAL_PACKAGE_NAME], [LOCAL_VERSION], [LOCAL_BUG_URI],
> +    [LOCAL_PACKAGE_TARNAME])
>
>  AC_PREREQ([2.59])
>
> @@ -433,7 +434,7 @@ AC_ARG_VAR([HELP2MAN_EXECUTABLE], [path to the `help2man' program])
>  if test -n "$HELP2MAN_EXECUTABLE"; then
>     help2man_enabled=true
>  else
> -    if test -f "$srcdir/bin/couchdb.1" -a -f "$srcdir/bin/couchjs.1"; then
> +    if test -f "$srcdir/bin/couchdb.1" -a -f "$srcdir/src/couchdb/priv/couchjs.1"; then
>         help2man_enabled=true
>     else
>         help2man_enabled=false
> @@ -486,7 +487,6 @@ AC_REVISION([LOCAL_VERSION])
>
>  AC_CONFIG_FILES([Makefile])
>  AC_CONFIG_FILES([bin/couch-config.tpl])
> -AC_CONFIG_FILES([bin/couchjs.tpl])
>  AC_CONFIG_FILES([bin/couchdb.tpl])
>  AC_CONFIG_FILES([bin/couchdb.bat.tpl])
>  AC_CONFIG_FILES([bin/Makefile])
>
> http://git-wip-us.apache.org/repos/asf/couchdb/blob/b09c2694/src/couchdb/priv/Makefile.am
> ----------------------------------------------------------------------
> diff --git a/src/couchdb/priv/Makefile.am b/src/couchdb/priv/Makefile.am
> index a0b3ff3..3d4b121 100644
> --- a/src/couchdb/priv/Makefile.am
> +++ b/src/couchdb/priv/Makefile.am
> @@ -45,6 +45,7 @@ couch_icu_driver_la_LDFLAGS += -no-undefined
>  endif
>
>  COUCHJS_SRCS = \
> +       couch_js/help.h \
>        couch_js/http.c \
>        couch_js/http.h \
>        couch_js/main.c \
> @@ -61,6 +62,19 @@ couchjs_LDADD = $(CURL_LIBS) $(JS_LIBS)
>  couchpriv_DATA = stat_descriptions.cfg
>  couchpriv_PROGRAMS = couchspawnkillable
>
> +if HELP2MAN
> +dist_man1_MANS = couchjs.1
> +endif
> +
> +HELP2MAN_OPTION=--no-info --help-option="-h" --version-option="-V"
> +
> +couchjs.1: couchjs
> +       touch $@
> +       if test -x "$(HELP2MAN_EXECUTABLE)"; then \
> +           $(HELP2MAN_EXECUTABLE) $(HELP2MAN_OPTION) \
> +               --name="$(package_name) JavaScript interpreter" ./couchjs --output $@; \
> +       fi
> +
>  %.cfg: %.cfg.in
>        cp $< $@
>
>
> http://git-wip-us.apache.org/repos/asf/couchdb/blob/b09c2694/src/couchdb/priv/couch_js/help.h
> ----------------------------------------------------------------------
> diff --git a/src/couchdb/priv/couch_js/help.h b/src/couchdb/priv/couch_js/help.h
> new file mode 100644
> index 0000000..e7c3af0
> --- /dev/null
> +++ b/src/couchdb/priv/couch_js/help.h
> @@ -0,0 +1,82 @@
> +// Licensed under the Apache License, Version 2.0 (the "License"); you may not
> +// use this file except in compliance with the License. You may obtain a copy of
> +// the License at
> +//
> +//   http://www.apache.org/licenses/LICENSE-2.0
> +//
> +// Unless required by applicable law or agreed to in writing, software
> +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
> +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
> +// License for the specific language governing permissions and limitations under
> +// the License.
> +
> +#ifndef COUCHJS_HELP_H
> +#define COUCHJS_HELP_H
> +
> +#include <libgen.h>
> +
> +#include "config.h"
> +
> +static const char VERSION_TEMPLATE[] =
> +    "%s - %s\n"
> +    "\n"
> +    "Licensed under the Apache License, Version 2.0 (the \"License\"); you may "
> +        "not use\n"
> +    "this file except in compliance with the License. You may obtain a copy of"
> +        "the\n"
> +    "License at\n"
> +    "\n"
> +    "  http://www.apache.org/licenses/LICENSE-2.0\n"
> +    "\n"
> +    "Unless required by applicable law or agreed to in writing, software "
> +        "distributed\n"
> +    "under the License is distributed on an \"AS IS\" BASIS, WITHOUT "
> +        "WARRANTIES OR\n"
> +    "CONDITIONS OF ANY KIND, either express or implied. See the License "
> +        "for the\n"
> +    "specific language governing permissions and limitations under the "
> +        "License.\n";
> +
> +static const char USAGE_TEMPLATE[] =
> +    "Usage: %s [FILE]\n"
> +    "\n"
> +    "The %s command runs the %s JavaScript interpreter.\n"
> +    "\n"
> +    "The exit status is 0 for success or 1 for failure.\n"
> +    "\n"
> +    "Options:\n"
> +    "\n"
> +    "  -h, --help          display a short help message and exit\n"
> +    "  -V, --version       display version information and exit\n"
> +    "  -H, --http          install %s cURL bindings (only avaiable\n"
> +    "                      if package was built with cURL available)\n"
> +    "  --stack-size=SIZE   specify that the interpreter should set the\n"
> +    "                      the stack quota for JS contexts to SIZE bytes\n"
> +    "\n"
> +    "Report bugs at <%s>.\n";
> +
> +#define BASENAME basename((char*)argv[0])
> +
> +#define couch_version(basename)  \
> +    fprintf(                     \
> +            stdout,              \
> +            VERSION_TEMPLATE,    \
> +            basename,            \
> +            PACKAGE_STRING)
> +
> +#define DISPLAY_VERSION couch_version(BASENAME)
> +
> +
> +#define couch_usage(basename) \
> +    fprintf(                                    \
> +            stdout,                             \
> +            USAGE_TEMPLATE,                     \
> +            basename,                           \
> +            basename,                           \
> +            PACKAGE_NAME,                       \
> +            basename,                           \
> +            PACKAGE_BUGREPORT)
> +
> +#define DISPLAY_USAGE couch_usage(BASENAME)
> +
> +#endif // Included help.h
>
> http://git-wip-us.apache.org/repos/asf/couchdb/blob/b09c2694/src/couchdb/priv/couch_js/util.c
> ----------------------------------------------------------------------
> diff --git a/src/couchdb/priv/couch_js/util.c b/src/couchdb/priv/couch_js/util.c
> index 070d717..ab1295f 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);
>     }
>
>
> http://git-wip-us.apache.org/repos/asf/couchdb/blob/b09c2694/src/couchdb/priv/couch_js/util.h
> ----------------------------------------------------------------------
> diff --git a/src/couchdb/priv/couch_js/util.h b/src/couchdb/priv/couch_js/util.h
> index 6caebfa..380ff84 100644
> --- a/src/couchdb/priv/couch_js/util.h
> +++ b/src/couchdb/priv/couch_js/util.h
> @@ -22,7 +22,6 @@ typedef struct {
>     char*       script;
>  } couch_args;
>
> -void couch_usage();
>  couch_args* couch_parse_args(int argc, const char* argv[]);
>  int couch_fgets(char* buf, int size, FILE* fp);
>  JSString* couch_readline(JSContext* cx, FILE* fp);
>
>