2844867
From e200556bfca9db7e496a50b62fa09a5f46f22b25 Mon Sep 17 00:00:00 2001
8ead4a2
From: Peter Lemenkov <lemenkov@gmail.com>
8ead4a2
Date: Sat, 20 Nov 2010 16:40:15 +0300
1374258
Subject: [PATCH 03/10] Remove bundled getopt
8ead4a2
8ead4a2
Signed-off-by: Peter Lemenkov <lemenkov@gmail.com>
8ead4a2
---
f16f160
 ebin/rebar.app |   3 +-
f16f160
 src/getopt.erl | 621 ---------------------------------------------------------
f41c5eb
 2 files changed, 1 insertion(+), 623 deletions(-)
8ead4a2
 delete mode 100644 src/getopt.erl
8ead4a2
8ead4a2
diff --git a/ebin/rebar.app b/ebin/rebar.app
ccc495b
index 9e000fa..cba8bf8 100644
8ead4a2
--- a/ebin/rebar.app
8ead4a2
+++ b/ebin/rebar.app
ccc495b
@@ -36,8 +36,7 @@
8ead4a2
               rebar_templater,
f41c5eb
               rebar_upgrade,
8ead4a2
               rebar_utils,
8ead4a2
-              rebar_xref,
8ead4a2
-              getopt ]},
f41c5eb
+              rebar_xref]},
8ead4a2
   {registered, []},
8ead4a2
   {applications, [kernel,
8ead4a2
                   stdlib,
8ead4a2
diff --git a/src/getopt.erl b/src/getopt.erl
8ead4a2
deleted file mode 100644
f41c5eb
index 175b7a5..0000000
8ead4a2
--- a/src/getopt.erl
8ead4a2
+++ /dev/null
f41c5eb
@@ -1,621 +0,0 @@
8ead4a2
-%%%-------------------------------------------------------------------
8ead4a2
-%%% @author Juan Jose Comellas <juanjo@comellas.org>
8ead4a2
-%%% @copyright (C) 2009 Juan Jose Comellas
8ead4a2
-%%% @doc Parses command line options with a format similar to that of GNU getopt.
8ead4a2
-%%% @end
8ead4a2
-%%%
8ead4a2
-%%% This source file is subject to the New BSD License. You should have received
8ead4a2
-%%% a copy of the New BSD license with this software. If not, it can be
8ead4a2
-%%% retrieved from: http://www.opensource.org/licenses/bsd-license.php
8ead4a2
-%%%-------------------------------------------------------------------
8ead4a2
--module(getopt).
8ead4a2
--author('juanjo@comellas.org').
8ead4a2
-
8ead4a2
--export([parse/2, usage/2, usage/3, usage/4]).
8ead4a2
-
f41c5eb
--export_type([arg_type/0,
f41c5eb
-	      arg_value/0,
f41c5eb
-	      arg_spec/0,
f41c5eb
-	      simple_option/0,
f41c5eb
-	      compound_option/0,
f41c5eb
-	      option/0,
f41c5eb
-	      option_spec/0]).
8ead4a2
-
8ead4a2
--define(TAB_LENGTH, 8).
8ead4a2
-%% Indentation of the help messages in number of tabs.
8ead4a2
--define(INDENTATION, 3).
8ead4a2
-
8ead4a2
-%% Position of each field in the option specification tuple.
8ead4a2
--define(OPT_NAME, 1).
8ead4a2
--define(OPT_SHORT, 2).
8ead4a2
--define(OPT_LONG, 3).
8ead4a2
--define(OPT_ARG, 4).
8ead4a2
--define(OPT_HELP, 5).
8ead4a2
-
8ead4a2
--define(IS_OPT_SPEC(Opt), (tuple_size(Opt) =:= ?OPT_HELP)).
8ead4a2
-
8ead4a2
-
8ead4a2
-%% Atom indicating the data type that an argument can be converted to.
8ead4a2
--type arg_type() :: 'atom' | 'binary' | 'boolean' | 'float' | 'integer' | 'string'.
8ead4a2
-%% Data type that an argument can be converted to.
8ead4a2
--type arg_value() :: atom() | binary() | boolean() | float() | integer() | string().
8ead4a2
-%% Argument specification.
8ead4a2
--type arg_spec() :: arg_type() | {arg_type(), arg_value()} | undefined.
8ead4a2
-%% Option type and optional default argument.
8ead4a2
--type simple_option() :: atom().
8ead4a2
--type compound_option() :: {atom(), arg_value()}.
8ead4a2
--type option() :: simple_option() | compound_option().
8ead4a2
-%% Command line option specification.
8ead4a2
--type option_spec() :: {
8ead4a2
-                   Name    :: atom(),
8ead4a2
-                   Short   :: char() | undefined,
8ead4a2
-                   Long    :: string() | undefined,
8ead4a2
-                   ArgSpec :: arg_spec(),
8ead4a2
-                   Help    :: string() | undefined
8ead4a2
-                  }.
f41c5eb
-%% Output streams
f41c5eb
--type output_stream() :: 'standard_io' | 'standard_error'.
8ead4a2
-
8ead4a2
-
8ead4a2
-%% @doc  Parse the command line options and arguments returning a list of tuples
8ead4a2
-%%       and/or atoms using the Erlang convention for sending options to a
8ead4a2
-%%       function.
8ead4a2
--spec parse([option_spec()], string() | [string()]) ->
8ead4a2
-    {ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data :: any()}}.
8ead4a2
-parse(OptSpecList, CmdLine) ->
8ead4a2
-    try
8ead4a2
-        Args = if
8ead4a2
-                   is_integer(hd(CmdLine)) ->
8ead4a2
-                       string:tokens(CmdLine, " \t\n");
8ead4a2
-                   true ->
8ead4a2
-                       CmdLine
8ead4a2
-               end,
8ead4a2
-        parse(OptSpecList, [], [], 0, Args)
8ead4a2
-    catch
8ead4a2
-        throw: {error, {_Reason, _Data}} = Error ->
8ead4a2
-            Error
8ead4a2
-    end.
8ead4a2
-
8ead4a2
-
8ead4a2
--spec parse([option_spec()], [option()], [string()], integer(), [string()]) ->
8ead4a2
-    {ok, {[option()], [string()]}}.
8ead4a2
-%% Process the option terminator.
8ead4a2
-parse(OptSpecList, OptAcc, ArgAcc, _ArgPos, ["--" | Tail]) ->
f41c5eb
-    %% Any argument present after the terminator is not considered an option.
8ead4a2
-    {ok, {lists:reverse(append_default_options(OptSpecList, OptAcc)), lists:reverse(ArgAcc, Tail)}};
8ead4a2
-%% Process long options.
f41c5eb
-parse(OptSpecList, OptAcc, ArgAcc, ArgPos, ["--" ++ OptArg = OptStr | Tail]) ->
f41c5eb
-    parse_long_option(OptSpecList, OptAcc, ArgAcc, ArgPos, Tail, OptStr, OptArg);
8ead4a2
-%% Process short options.
f41c5eb
-parse(OptSpecList, OptAcc, ArgAcc, ArgPos, ["-" ++ ([_Char | _] = OptArg) = OptStr | Tail]) ->
f41c5eb
-    parse_short_option(OptSpecList, OptAcc, ArgAcc, ArgPos, Tail, OptStr, OptArg);
8ead4a2
-%% Process non-option arguments.
8ead4a2
-parse(OptSpecList, OptAcc, ArgAcc, ArgPos, [Arg | Tail]) ->
8ead4a2
-    case find_non_option_arg(OptSpecList, ArgPos) of
8ead4a2
-        {value, OptSpec} when ?IS_OPT_SPEC(OptSpec) ->
f41c5eb
-            parse(OptSpecList, add_option_with_arg(OptSpec, Arg, OptAcc), ArgAcc, ArgPos + 1, Tail);
8ead4a2
-        false ->
8ead4a2
-            parse(OptSpecList, OptAcc, [Arg | ArgAcc], ArgPos, Tail)
8ead4a2
-    end;
8ead4a2
-parse(OptSpecList, OptAcc, ArgAcc, _ArgPos, []) ->
f41c5eb
-    %% Once we have completed gathering the options we add the ones that were
f41c5eb
-    %% not present but had default arguments in the specification.
8ead4a2
-    {ok, {lists:reverse(append_default_options(OptSpecList, OptAcc)), lists:reverse(ArgAcc)}}.
8ead4a2
-
8ead4a2
-
8ead4a2
-%% @doc Parse a long option, add it to the option accumulator and continue
8ead4a2
-%%      parsing the rest of the arguments recursively.
8ead4a2
-%%      A long option can have the following syntax:
8ead4a2
-%%        --foo      Single option 'foo', no argument
8ead4a2
-%%        --foo=bar  Single option 'foo', argument "bar"
8ead4a2
-%%        --foo bar  Single option 'foo', argument "bar"
f41c5eb
--spec parse_long_option([option_spec()], [option()], [string()], integer(), [string()], string(), string()) ->
8ead4a2
-          {ok, {[option()], [string()]}}.
f41c5eb
-parse_long_option(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptStr, OptArg) ->
8ead4a2
-    case split_assigned_arg(OptArg) of
8ead4a2
-        {Long, Arg} ->
f41c5eb
-            %% Get option that has its argument within the same string
f41c5eb
-            %% separated by an equal ('=') character (e.g. "--port=1000").
f41c5eb
-            parse_long_option_assigned_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptStr, Long, Arg);
8ead4a2
-
8ead4a2
-        Long ->
f41c5eb
-            case lists:keyfind(Long, ?OPT_LONG, OptSpecList) of
f41c5eb
-                {Name, _Short, Long, undefined, _Help} ->
8ead4a2
-                    parse(OptSpecList, [Name | OptAcc], ArgAcc, ArgPos, Args);
f41c5eb
-
f41c5eb
-                {_Name, _Short, Long, _ArgSpec, _Help} = OptSpec ->
f41c5eb
-                    %% The option argument string is empty, but the option requires
f41c5eb
-                    %% an argument, so we look into the next string in the list.
f41c5eb
-                    %% e.g ["--port", "1000"]
f41c5eb
-                    parse_long_option_next_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptSpec);
8ead4a2
-                false ->
8ead4a2
-                    throw({error, {invalid_option, OptStr}})
8ead4a2
-            end
8ead4a2
-    end.
8ead4a2
-
8ead4a2
-
8ead4a2
-%% @doc Parse an option where the argument is 'assigned' in the same string using
8ead4a2
-%%      the '=' character, add it to the option accumulator and continue parsing the
8ead4a2
-%%      rest of the arguments recursively. This syntax is only valid for long options.
f41c5eb
--spec parse_long_option_assigned_arg([option_spec()], [option()], [string()], integer(),
f41c5eb
-                                     [string()], string(), string(), string()) ->
f41c5eb
-                                            {ok, {[option()], [string()]}}.
f41c5eb
-parse_long_option_assigned_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptStr, Long, Arg) ->
f41c5eb
-    case lists:keyfind(Long, ?OPT_LONG, OptSpecList) of
f41c5eb
-        {_Name, _Short, Long, ArgSpec, _Help} = OptSpec ->
8ead4a2
-            case ArgSpec of
8ead4a2
-                undefined ->
8ead4a2
-                    throw({error, {invalid_option_arg, OptStr}});
8ead4a2
-                _ ->
f41c5eb
-                    parse(OptSpecList, add_option_with_assigned_arg(OptSpec, Arg, OptAcc), ArgAcc, ArgPos, Args)
8ead4a2
-            end;
8ead4a2
-        false ->
8ead4a2
-            throw({error, {invalid_option, OptStr}})
8ead4a2
-    end.
8ead4a2
-
8ead4a2
-
8ead4a2
-%% @doc Split an option string that may contain an option with its argument
8ead4a2
-%%      separated by an equal ('=') character (e.g. "port=1000").
8ead4a2
--spec split_assigned_arg(string()) -> {Name :: string(), Arg :: string()} | string().
8ead4a2
-split_assigned_arg(OptStr) ->
8ead4a2
-    split_assigned_arg(OptStr, OptStr, []).
8ead4a2
-
f41c5eb
-split_assigned_arg(_OptStr, "=" ++ Tail, Acc) ->
8ead4a2
-    {lists:reverse(Acc), Tail};
8ead4a2
-split_assigned_arg(OptStr, [Char | Tail], Acc) ->
8ead4a2
-    split_assigned_arg(OptStr, Tail, [Char | Acc]);
8ead4a2
-split_assigned_arg(OptStr, [], _Acc) ->
8ead4a2
-    OptStr.
8ead4a2
-
8ead4a2
-
f41c5eb
-%% @doc Retrieve the argument for an option from the next string in the list of
f41c5eb
-%%      command-line parameters or set the value of the argument from the argument
f41c5eb
-%%      specification (for boolean and integer arguments), if possible.
f41c5eb
-parse_long_option_next_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, {Name, _Short, _Long, ArgSpec, _Help} = OptSpec) ->
f41c5eb
-    ArgSpecType = arg_spec_type(ArgSpec),
f41c5eb
-    case Args =:= [] orelse is_implicit_arg(ArgSpecType, hd(Args)) of
f41c5eb
-        true ->
f41c5eb
-            parse(OptSpecList, add_option_with_implicit_arg(OptSpec, OptAcc), ArgAcc, ArgPos, Args);
f41c5eb
-        false ->
f41c5eb
-            [Arg | Tail] = Args,
f41c5eb
-            try
f41c5eb
-                parse(OptSpecList, [{Name, to_type(ArgSpecType, Arg)} | OptAcc], ArgAcc, ArgPos, Tail)
f41c5eb
-            catch
f41c5eb
-                error:_ ->
f41c5eb
-                    throw({error, {invalid_option_arg, {Name, Arg}}})
f41c5eb
-            end
f41c5eb
-    end.
f41c5eb
-
f41c5eb
-
8ead4a2
-%% @doc Parse a short option, add it to the option accumulator and continue
8ead4a2
-%%      parsing the rest of the arguments recursively.
8ead4a2
-%%      A short option can have the following syntax:
8ead4a2
-%%        -a       Single option 'a', no argument or implicit boolean argument
8ead4a2
-%%        -a foo   Single option 'a', argument "foo"
8ead4a2
-%%        -afoo    Single option 'a', argument "foo"
8ead4a2
-%%        -abc     Multiple options: 'a'; 'b'; 'c'
8ead4a2
-%%        -bcafoo  Multiple options: 'b'; 'c'; 'a' with argument "foo"
f41c5eb
-%%        -aaa     Multiple repetitions of option 'a' (only valid for options with integer arguments)
f41c5eb
--spec parse_short_option([option_spec()], [option()], [string()], integer(), [string()], string(), string()) ->
8ead4a2
-    {ok, {[option()], [string()]}}.
f41c5eb
-parse_short_option(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptStr, OptArg) ->
f41c5eb
-    parse_short_option(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptStr, first, OptArg).
8ead4a2
-
f41c5eb
-parse_short_option(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptStr, OptPos, [Short | Arg]) ->
f41c5eb
-    case lists:keyfind(Short, ?OPT_SHORT, OptSpecList) of
f41c5eb
-        {Name, Short, _Long, undefined, _Help} ->
f41c5eb
-            parse_short_option(OptSpecList, [Name | OptAcc], ArgAcc, ArgPos, Args, OptStr, first, Arg);
f41c5eb
-
f41c5eb
-        {_Name, Short, _Long, ArgSpec, _Help} = OptSpec ->
f41c5eb
-            %% The option has a specification, so it requires an argument.
8ead4a2
-            case Arg of
8ead4a2
-                [] ->
f41c5eb
-                    %% The option argument string is empty, but the option requires
f41c5eb
-                    %% an argument, so we look into the next string in the list.
f41c5eb
-                    parse_short_option_next_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptSpec, OptPos);
8ead4a2
-
8ead4a2
-                _ ->
8ead4a2
-                    case is_valid_arg(ArgSpec, Arg) of
8ead4a2
-                        true ->
f41c5eb
-                            parse(OptSpecList, add_option_with_arg(OptSpec, Arg, OptAcc), ArgAcc, ArgPos, Args);
8ead4a2
-                        _ ->
f41c5eb
-                            NewOptAcc = case OptPos of
f41c5eb
-                                            first -> add_option_with_implicit_arg(OptSpec, OptAcc);
f41c5eb
-                                            _     -> add_option_with_implicit_incrementable_arg(OptSpec, OptAcc)
f41c5eb
-                                        end,
f41c5eb
-                            parse_short_option(OptSpecList, NewOptAcc, ArgAcc, ArgPos, Args, OptStr, next, Arg)
8ead4a2
-                    end
8ead4a2
-            end;
8ead4a2
-
8ead4a2
-        false ->
8ead4a2
-            throw({error, {invalid_option, OptStr}})
8ead4a2
-    end;
f41c5eb
-parse_short_option(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, _OptStr, _OptPos, []) ->
8ead4a2
-    parse(OptSpecList, OptAcc, ArgAcc, ArgPos, Args).
8ead4a2
-
8ead4a2
-
8ead4a2
-%% @doc Retrieve the argument for an option from the next string in the list of
f41c5eb
-%%      command-line parameters or set the value of the argument from the argument
f41c5eb
-%%      specification (for boolean and integer arguments), if possible.
f41c5eb
-parse_short_option_next_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, {Name, _Short, _Long, ArgSpec, _Help} = OptSpec, OptPos) ->
f41c5eb
-    case Args =:= [] orelse is_implicit_arg(ArgSpec, hd(Args)) of
f41c5eb
-        true when OptPos =:= first ->
f41c5eb
-            parse(OptSpecList, add_option_with_implicit_arg(OptSpec, OptAcc), ArgAcc, ArgPos, Args);
8ead4a2
-        true ->
f41c5eb
-            parse(OptSpecList, add_option_with_implicit_incrementable_arg(OptSpec, OptAcc), ArgAcc, ArgPos, Args);
f41c5eb
-        false ->
f41c5eb
-            [Arg | Tail] = Args,
f41c5eb
-            try
f41c5eb
-                parse(OptSpecList, [{Name, to_type(ArgSpec, Arg)} | OptAcc], ArgAcc, ArgPos, Tail)
f41c5eb
-            catch
f41c5eb
-                error:_ ->
f41c5eb
-                    throw({error, {invalid_option_arg, {Name, Arg}}})
f41c5eb
-            end
8ead4a2
-    end.
f41c5eb
-
8ead4a2
-
8ead4a2
-%% @doc Find the option for the discrete argument in position specified in the
8ead4a2
-%%      Pos argument.
8ead4a2
--spec find_non_option_arg([option_spec()], integer()) -> {value, option_spec()} | false.
8ead4a2
-find_non_option_arg([{_Name, undefined, undefined, _ArgSpec, _Help} = OptSpec | _Tail], 0) ->
8ead4a2
-     {value, OptSpec};
8ead4a2
-find_non_option_arg([{_Name, undefined, undefined, _ArgSpec, _Help} | Tail], Pos) ->
8ead4a2
-    find_non_option_arg(Tail, Pos - 1);
8ead4a2
-find_non_option_arg([_Head | Tail], Pos) ->
8ead4a2
-    find_non_option_arg(Tail, Pos);
8ead4a2
-find_non_option_arg([], _Pos) ->
8ead4a2
-    false.
8ead4a2
-
8ead4a2
-
8ead4a2
-%% @doc Append options that were not present in the command line arguments with
8ead4a2
-%%      their default arguments.
8ead4a2
--spec append_default_options([option_spec()], [option()]) -> [option()].
8ead4a2
-append_default_options([{Name, _Short, _Long, {_Type, DefaultArg}, _Help} | Tail], OptAcc) ->
8ead4a2
-    append_default_options(Tail,
8ead4a2
-               case lists:keymember(Name, 1, OptAcc) of
8ead4a2
-                   false ->
8ead4a2
-                       [{Name, DefaultArg} | OptAcc];
8ead4a2
-                   _ ->
8ead4a2
-                       OptAcc
8ead4a2
-               end);
8ead4a2
-%% For options with no default argument.
8ead4a2
-append_default_options([_Head | Tail], OptAcc) ->
8ead4a2
-    append_default_options(Tail, OptAcc);
8ead4a2
-append_default_options([], OptAcc) ->
8ead4a2
-    OptAcc.
8ead4a2
-
8ead4a2
-
f41c5eb
-%% @doc Add an option with argument converting it to the data type indicated by the
f41c5eb
-%%      argument specification.
f41c5eb
--spec add_option_with_arg(option_spec(), string(), [option()]) -> [option()].
f41c5eb
-add_option_with_arg({Name, _Short, _Long, ArgSpec, _Help} = OptSpec, Arg, OptAcc) ->
f41c5eb
-    case is_valid_arg(ArgSpec, Arg) of
f41c5eb
-        true ->
f41c5eb
-            try
f41c5eb
-                [{Name, to_type(ArgSpec, Arg)} | OptAcc]
f41c5eb
-            catch
f41c5eb
-                error:_ ->
f41c5eb
-                    throw({error, {invalid_option_arg, {Name, Arg}}})
f41c5eb
-            end;
f41c5eb
-        false ->
f41c5eb
-            add_option_with_implicit_arg(OptSpec, OptAcc)
8ead4a2
-    end.
8ead4a2
-
8ead4a2
-
f41c5eb
-%% @doc Add an option with argument that was part of an assignment expression
f41c5eb
-%%      (e.g. "--verbose=3") converting it to the data type indicated by the
f41c5eb
-%%      argument specification.
f41c5eb
--spec add_option_with_assigned_arg(option_spec(), string(), [option()]) -> [option()].
f41c5eb
-add_option_with_assigned_arg({Name, _Short, _Long, ArgSpec, _Help}, Arg, OptAcc) ->
8ead4a2
-    try
f41c5eb
-        [{Name, to_type(ArgSpec, Arg)} | OptAcc]
8ead4a2
-    catch
8ead4a2
-        error:_ ->
8ead4a2
-            throw({error, {invalid_option_arg, {Name, Arg}}})
8ead4a2
-    end.
8ead4a2
-
8ead4a2
-
f41c5eb
-%% @doc Add an option that required an argument but did not have one. Some data
f41c5eb
-%%      types (boolean, integer) allow implicit or assumed arguments.
f41c5eb
--spec add_option_with_implicit_arg(option_spec(), [option()]) -> [option()].
f41c5eb
-add_option_with_implicit_arg({Name, _Short, _Long, ArgSpec, _Help}, OptAcc) ->
f41c5eb
-    case arg_spec_type(ArgSpec) of
f41c5eb
-        boolean ->
f41c5eb
-            %% Special case for boolean arguments: if there is no argument we
f41c5eb
-            %% set the value to 'true'.
f41c5eb
-            [{Name, true} | OptAcc];
f41c5eb
-        integer ->
f41c5eb
-            %% Special case for integer arguments: if the option had not been set
f41c5eb
-            %% before we set the value to 1. This is needed to support options like
f41c5eb
-            %% "-v" to return something like {verbose, 1}.
f41c5eb
-            [{Name, 1} | OptAcc];
f41c5eb
-        _ ->
f41c5eb
-            throw({error, {missing_option_arg, Name}})
f41c5eb
-    end.
f41c5eb
-
f41c5eb
-
f41c5eb
-%% @doc Add an option with an implicit or assumed argument.
f41c5eb
--spec add_option_with_implicit_incrementable_arg(option_spec() | arg_spec(), [option()]) -> [option()].
f41c5eb
-add_option_with_implicit_incrementable_arg({Name, _Short, _Long, ArgSpec, _Help}, OptAcc) ->
f41c5eb
-    case arg_spec_type(ArgSpec) of
f41c5eb
-        boolean ->
f41c5eb
-            %% Special case for boolean arguments: if there is no argument we
f41c5eb
-            %% set the value to 'true'.
f41c5eb
-            [{Name, true} | OptAcc];
f41c5eb
-        integer ->
f41c5eb
-            %% Special case for integer arguments: if the option had not been set
f41c5eb
-            %% before we set the value to 1; if not we increment the previous value
f41c5eb
-            %% the option had. This is needed to support options like "-vvv" to
f41c5eb
-            %% return something like {verbose, 3}.
f41c5eb
-            case OptAcc of
f41c5eb
-                [{Name, Count} | Tail] ->
f41c5eb
-                    [{Name, Count + 1} | Tail];
f41c5eb
-                _ ->
f41c5eb
-                    [{Name, 1} | OptAcc]
f41c5eb
-            end;
f41c5eb
-        _ ->
f41c5eb
-            throw({error, {missing_option_arg, Name}})
f41c5eb
-    end.
f41c5eb
-
f41c5eb
-
8ead4a2
-%% @doc Retrieve the data type form an argument specification.
8ead4a2
--spec arg_spec_type(arg_spec()) -> arg_type() | undefined.
8ead4a2
-arg_spec_type({Type, _DefaultArg}) ->
8ead4a2
-    Type;
8ead4a2
-arg_spec_type(Type) when is_atom(Type) ->
8ead4a2
-    Type.
8ead4a2
-
8ead4a2
-
8ead4a2
-%% @doc Convert an argument string to its corresponding data type.
f41c5eb
--spec to_type(arg_spec() | arg_type(), string()) -> arg_value().
f41c5eb
-to_type({Type, _DefaultArg}, Arg) ->
f41c5eb
-    to_type(Type, Arg);
8ead4a2
-to_type(binary, Arg) ->
8ead4a2
-    list_to_binary(Arg);
8ead4a2
-to_type(atom, Arg) ->
8ead4a2
-    list_to_atom(Arg);
8ead4a2
-to_type(integer, Arg) ->
8ead4a2
-    list_to_integer(Arg);
8ead4a2
-to_type(float, Arg) ->
8ead4a2
-    list_to_float(Arg);
8ead4a2
-to_type(boolean, Arg) ->
8ead4a2
-    LowerArg = string:to_lower(Arg),
8ead4a2
-    case is_arg_true(LowerArg) of
8ead4a2
-        true ->
8ead4a2
-            true;
8ead4a2
-        _ ->
8ead4a2
-            case is_arg_false(LowerArg) of
8ead4a2
-                true ->
8ead4a2
-                    false;
8ead4a2
-                false ->
8ead4a2
-                    erlang:error(badarg)
8ead4a2
-            end
8ead4a2
-    end;
8ead4a2
-to_type(_Type, Arg) ->
8ead4a2
-    Arg.
8ead4a2
-
8ead4a2
-
8ead4a2
--spec is_arg_true(string()) -> boolean().
8ead4a2
-is_arg_true(Arg) ->
8ead4a2
-    (Arg =:= "true") orelse (Arg =:= "t") orelse
8ead4a2
-    (Arg =:= "yes") orelse (Arg =:= "y") orelse
8ead4a2
-    (Arg =:= "on") orelse (Arg =:= "enabled") orelse
8ead4a2
-    (Arg =:= "1").
8ead4a2
-
f41c5eb
-
8ead4a2
--spec is_arg_false(string()) -> boolean().
8ead4a2
-is_arg_false(Arg) ->
8ead4a2
-    (Arg =:= "false") orelse (Arg =:= "f") orelse
8ead4a2
-    (Arg =:= "no") orelse (Arg =:= "n") orelse
8ead4a2
-    (Arg =:= "off") orelse (Arg =:= "disabled") orelse
8ead4a2
-    (Arg =:= "0").
8ead4a2
-
8ead4a2
-
8ead4a2
--spec is_valid_arg(arg_spec(), nonempty_string()) -> boolean().
8ead4a2
-is_valid_arg({Type, _DefaultArg}, Arg) ->
8ead4a2
-    is_valid_arg(Type, Arg);
8ead4a2
-is_valid_arg(boolean, Arg) ->
8ead4a2
-    is_boolean_arg(Arg);
8ead4a2
-is_valid_arg(integer, Arg) ->
f41c5eb
-    is_non_neg_integer_arg(Arg);
8ead4a2
-is_valid_arg(float, Arg) ->
f41c5eb
-    is_non_neg_float_arg(Arg);
8ead4a2
-is_valid_arg(_Type, _Arg) ->
8ead4a2
-    true.
8ead4a2
-
8ead4a2
-
f41c5eb
--spec is_implicit_arg(arg_spec(), nonempty_string()) -> boolean().
f41c5eb
-is_implicit_arg({Type, _DefaultArg}, Arg) ->
f41c5eb
-    is_implicit_arg(Type, Arg);
f41c5eb
-is_implicit_arg(boolean, Arg) ->
f41c5eb
-    not is_boolean_arg(Arg);
f41c5eb
-is_implicit_arg(integer, Arg) ->
f41c5eb
-    not is_integer_arg(Arg);
f41c5eb
-is_implicit_arg(_Type, _Arg) ->
f41c5eb
-    false.
f41c5eb
-
f41c5eb
-
8ead4a2
--spec is_boolean_arg(string()) -> boolean().
8ead4a2
-is_boolean_arg(Arg) ->
8ead4a2
-    LowerArg = string:to_lower(Arg),
8ead4a2
-    is_arg_true(LowerArg) orelse is_arg_false(LowerArg).
8ead4a2
-
8ead4a2
-
8ead4a2
--spec is_integer_arg(string()) -> boolean().
f41c5eb
-is_integer_arg("-" ++ Tail) ->
f41c5eb
-    is_non_neg_integer_arg(Tail);
f41c5eb
-is_integer_arg(Arg) ->
f41c5eb
-    is_non_neg_integer_arg(Arg).
f41c5eb
-
f41c5eb
-
f41c5eb
--spec is_non_neg_integer_arg(string()) -> boolean().
f41c5eb
-is_non_neg_integer_arg([Head | Tail]) when Head >= $0, Head =< $9 ->
f41c5eb
-    is_non_neg_integer_arg(Tail);
f41c5eb
-is_non_neg_integer_arg([_Head | _Tail]) ->
8ead4a2
-    false;
f41c5eb
-is_non_neg_integer_arg([]) ->
8ead4a2
-    true.
8ead4a2
-
8ead4a2
-
f41c5eb
--spec is_non_neg_float_arg(string()) -> boolean().
f41c5eb
-is_non_neg_float_arg([Head | Tail]) when (Head >= $0 andalso Head =< $9) orelse Head =:= $. ->
f41c5eb
-    is_non_neg_float_arg(Tail);
f41c5eb
-is_non_neg_float_arg([_Head | _Tail]) ->
8ead4a2
-    false;
f41c5eb
-is_non_neg_float_arg([]) ->
8ead4a2
-    true.
8ead4a2
-
f41c5eb
-
f41c5eb
-%% @doc  Show a message on standard_error indicating the command line options and
8ead4a2
-%%       arguments that are supported by the program.
8ead4a2
--spec usage([option_spec()], string()) -> ok.
8ead4a2
-usage(OptSpecList, ProgramName) ->
f41c5eb
-	usage(OptSpecList, ProgramName, standard_error).
8ead4a2
-
8ead4a2
-
f41c5eb
-%% @doc  Show a message on standard_error or standard_io indicating the command line options and
f41c5eb
-%%       arguments that are supported by the program.
f41c5eb
--spec usage([option_spec()], string(), output_stream() | string()) -> ok.
f41c5eb
-usage(OptSpecList, ProgramName, OutputStream) when is_atom(OutputStream) ->
f41c5eb
-    io:format(OutputStream, "Usage: ~s~s~n~n~s~n",
f41c5eb
-              [ProgramName, usage_cmd_line(OptSpecList), usage_options(OptSpecList)]);
f41c5eb
-%% @doc  Show a message on standard_error indicating the command line options and
8ead4a2
-%%       arguments that are supported by the program. The CmdLineTail argument
8ead4a2
-%%       is a string that is added to the end of the usage command line.
8ead4a2
-usage(OptSpecList, ProgramName, CmdLineTail) ->
f41c5eb
-	usage(OptSpecList, ProgramName, CmdLineTail, standard_error).
8ead4a2
-
8ead4a2
-
f41c5eb
-%% @doc  Show a message on standard_error or standard_io indicating the command line options and
f41c5eb
-%%       arguments that are supported by the program. The CmdLineTail argument
f41c5eb
-%%       is a string that is added to the end of the usage command line.
f41c5eb
--spec usage([option_spec()], string(), string(), output_stream() | [{string(), string()}]) -> ok.
f41c5eb
-usage(OptSpecList, ProgramName, CmdLineTail, OutputStream) when is_atom(OutputStream) ->
f41c5eb
-	io:format(OutputStream, "Usage: ~s~s ~s~n~n~s~n",
f41c5eb
-              [ProgramName, usage_cmd_line(OptSpecList), CmdLineTail, usage_options(OptSpecList)]);
f41c5eb
-%% @doc  Show a message on standard_error indicating the command line options and
8ead4a2
-%%       arguments that are supported by the program. The CmdLineTail and OptionsTail
8ead4a2
-%%       arguments are a string that is added to the end of the usage command line
8ead4a2
-%%       and a list of tuples that are added to the end of the options' help lines.
8ead4a2
-usage(OptSpecList, ProgramName, CmdLineTail, OptionsTail) ->
f41c5eb
-	usage(OptSpecList, ProgramName, CmdLineTail, OptionsTail, standard_error).
f41c5eb
-
f41c5eb
-
f41c5eb
-%% @doc  Show a message on standard_error or standard_io indicating the command line options and
f41c5eb
-%%       arguments that are supported by the program. The CmdLineTail and OptionsTail
f41c5eb
-%%       arguments are a string that is added to the end of the usage command line
f41c5eb
-%%       and a list of tuples that are added to the end of the options' help lines.
f41c5eb
--spec usage([option_spec()], string(), string(), [{string(), string()}], output_stream()) -> ok.
f41c5eb
-usage(OptSpecList, ProgramName, CmdLineTail, OptionsTail, OutputStream) ->
8ead4a2
-    UsageOptions = lists:foldl(
8ead4a2
-                     fun ({Prefix, Help}, Acc) ->
8ead4a2
-                             add_option_help(Prefix, Help, Acc)
8ead4a2
-                     end, usage_options_reverse(OptSpecList, []), OptionsTail),
f41c5eb
-    io:format(OutputStream, "Usage: ~s~s ~s~n~n~s~n",
8ead4a2
-              [ProgramName, usage_cmd_line(OptSpecList), CmdLineTail,
8ead4a2
-               lists:flatten(lists:reverse(UsageOptions))]).
8ead4a2
-
8ead4a2
-
8ead4a2
-%% @doc Return a string with the syntax for the command line options and
8ead4a2
-%%      arguments.
8ead4a2
--spec usage_cmd_line([option_spec()]) -> string().
8ead4a2
-usage_cmd_line(OptSpecList) ->
8ead4a2
-    usage_cmd_line(OptSpecList, []).
8ead4a2
-
8ead4a2
-usage_cmd_line([{Name, Short, Long, ArgSpec, _Help} | Tail], Acc) ->
8ead4a2
-    CmdLine =
8ead4a2
-        case ArgSpec of
8ead4a2
-            undefined ->
8ead4a2
-                if
f41c5eb
-                    %% For options with short form and no argument.
8ead4a2
-                    Short =/= undefined ->
8ead4a2
-                        [$\s, $[, $-, Short, $]];
f41c5eb
-                    %% For options with only long form and no argument.
8ead4a2
-                    Long =/= undefined ->
8ead4a2
-                        [$\s, $[, $-, $-, Long, $]];
8ead4a2
-                    true ->
8ead4a2
-                        []
8ead4a2
-                end;
8ead4a2
-            _ ->
8ead4a2
-                if
f41c5eb
-                    %% For options with short form and argument.
8ead4a2
-                    Short =/= undefined ->
8ead4a2
-                        [$\s, $[, $-, Short, $\s, $<, atom_to_list(Name), $>, $]];
f41c5eb
-                    %% For options with only long form and argument.
8ead4a2
-                    Long =/= undefined ->
8ead4a2
-                        [$\s, $[, $-, $-, Long, $\s, $<, atom_to_list(Name), $>, $]];
f41c5eb
-                    %% For options with neither short nor long form and argument.
8ead4a2
-                    true ->
8ead4a2
-                        [$\s, $<, atom_to_list(Name), $>]
8ead4a2
-                end
8ead4a2
-        end,
8ead4a2
-    usage_cmd_line(Tail, [CmdLine | Acc]);
8ead4a2
-usage_cmd_line([], Acc) ->
8ead4a2
-    lists:flatten(lists:reverse(Acc)).
8ead4a2
-
8ead4a2
-
8ead4a2
-%% @doc Return a string with the help message for each of the options and
8ead4a2
-%%      arguments.
8ead4a2
--spec usage_options([option_spec()]) -> string().
8ead4a2
-usage_options(OptSpecList) ->
8ead4a2
-    lists:flatten(lists:reverse(usage_options_reverse(OptSpecList, []))).
8ead4a2
-
8ead4a2
-usage_options_reverse([{Name, Short, Long, _ArgSpec, Help} | Tail], Acc) ->
f41c5eb
-    Prefix =
f41c5eb
-        case Long of
8ead4a2
-            undefined ->
8ead4a2
-                case Short of
f41c5eb
-                    %% Neither short nor long form (non-option argument).
8ead4a2
-                    undefined ->
8ead4a2
-                        [$<, atom_to_list(Name), $>];
f41c5eb
-                    %% Only short form.
8ead4a2
-                    _ ->
8ead4a2
-                        [$-, Short]
8ead4a2
-                end;
8ead4a2
-            _ ->
8ead4a2
-                case Short of
f41c5eb
-                    %% Only long form.
8ead4a2
-                    undefined ->
8ead4a2
-                        [$-, $- | Long];
f41c5eb
-                    %% Both short and long form.
8ead4a2
-                    _ ->
8ead4a2
-                        [$-, Short, $,, $\s, $-, $- | Long]
8ead4a2
-                end
8ead4a2
-        end,
8ead4a2
-    usage_options_reverse(Tail, add_option_help(Prefix, Help, Acc));
8ead4a2
-usage_options_reverse([], Acc) ->
8ead4a2
-    Acc.
8ead4a2
-
8ead4a2
-
8ead4a2
-%% @doc Add the help message corresponding to an option specification to a list
8ead4a2
-%%      with the correct indentation.
8ead4a2
--spec add_option_help(Prefix :: string(), Help :: string(), Acc :: string()) -> string().
8ead4a2
-add_option_help(Prefix, Help, Acc) when is_list(Help), Help =/= [] ->
8ead4a2
-    FlatPrefix = lists:flatten(Prefix),
8ead4a2
-    case ((?INDENTATION * ?TAB_LENGTH) - 2 - length(FlatPrefix)) of
8ead4a2
-        TabSize when TabSize > 0 ->
8ead4a2
-            Tab = lists:duplicate(ceiling(TabSize / ?TAB_LENGTH), $\t),
8ead4a2
-            [[$\s, $\s, FlatPrefix, Tab, Help, $\n] | Acc];
8ead4a2
-        _ ->
8ead4a2
-            % The indentation for the option description is 3 tabs (i.e. 24 characters)
8ead4a2
-            % IMPORTANT: Change the number of tabs below if you change the
8ead4a2
-            %            value of the INDENTATION macro.
8ead4a2
-            [[$\t, $\t, $\t, Help, $\n], [$\s, $\s, FlatPrefix, $\n] | Acc]
8ead4a2
-    end;
8ead4a2
-add_option_help(_Opt, _Prefix, Acc) ->
8ead4a2
-    Acc.
8ead4a2
-
8ead4a2
-
8ead4a2
-
8ead4a2
-%% @doc Return the smallest integral value not less than the argument.
8ead4a2
--spec ceiling(float()) -> integer().
8ead4a2
-ceiling(X) ->
8ead4a2
-    T = erlang:trunc(X),
8ead4a2
-    case (X - T) of
8ead4a2
-        % Neg when Neg < 0 ->
8ead4a2
-        %    T;
8ead4a2
-        Pos when Pos > 0 ->
8ead4a2
-            T + 1;
8ead4a2
-        _ ->
8ead4a2
-            T
8ead4a2
-    end.
8ead4a2
-- 
ccc495b
1.8.1.4
8ead4a2