aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2013-08-18 13:05:32 +0200
committerMattias Andrée <maandree@operamail.com>2013-08-18 13:05:32 +0200
commitf8a5ac5337702119cb0f285879953cbdeceaeb20 (patch)
tree9e3d868dac98450e90ca8afd689424f26de603dd
parentforgot to update the version (diff)
downloadargparser-f8a5ac5337702119cb0f285879953cbdeceaeb20.tar.gz
argparser-f8a5ac5337702119cb0f285879953cbdeceaeb20.tar.bz2
argparser-f8a5ac5337702119cb0f285879953cbdeceaeb20.tar.xz
add alternative mode
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r--info/argparser.texinfo18
-rw-r--r--src/argparser.bash4
-rw-r--r--src/argparser.c11
-rw-r--r--src/argparser.h21
-rw-r--r--src/argparser.py9
-rw-r--r--src/argparser/ArgParser.java15
-rw-r--r--src/test.c2
7 files changed, 55 insertions, 25 deletions
diff --git a/info/argparser.texinfo b/info/argparser.texinfo
index 76c9168..540b5fb 100644
--- a/info/argparser.texinfo
+++ b/info/argparser.texinfo
@@ -120,11 +120,13 @@ of the option and may span multiple lines but should only do so if the
lines below the first is just extra details.
When you have populated your @code{ArgParser} with options, it is time
-to parse arguments, it is done with the method @code{parses} that optional
+to parse arguments, it is done with the method @code{parse} that optional
takes and list of arguments. If you choose to use a list of arguments
rather than letting @code{ArgParser} use arguments used to start the
-program, the first ement will not be parsed as it is assumed to be the
-executable.
+program, the first element will not be parsed as it is assumed to be the
+executable. If you want long options with just one dash or plus, which
+disables short options, you can pass @code{True}, as the second (option)
+argument.
If you now want to use any option alternative rather than just the
primary (using just the primary is good to keep your code consistent)
@@ -148,7 +150,7 @@ is a return code can be provided if you want the program to exit if there
are out of context option. Remember that you should also check that the
number of times an option is used is acceptable.
-After running @code{parses}, your @code{ArgParser} has five attributes:
+After running @code{parse}, your @code{ArgParser} has five attributes:
@code{unrecognisedCount}, the number of unrecognised options; and
@code{message}, the join of @code{files} which is all arguments not
associated with an option, @code{arguments} the parsed arguments, and
@@ -185,7 +187,9 @@ Further @code{support_alternatives}, @code{test_exclusiveness},
without @code{min} is named @code{testFilesMax} and @code{testFiles}
without @code{max} is named @code{testFilesMin}.
-@code{parse} takes an array of arguments, which excludes the executable.
+@code{parse} takes an array of arguments, which excludes the executable,
+and optionally whether to use long options with one dash or plus and no
+short options.
@code{message} is a @code{String}, @code{files} is a @code{ArrayList<String>},
@code{unrecognisedCount} is a @code{int}, @code{arguments} is a @code{String[]},
@@ -215,7 +219,9 @@ namespace @code{args}. When you are done using ArgParser you should
free its resources using function @code{args_dispose}.
@code{args_init} corresponds the the @code{ArgParser} constructor in
-the Python version, but all parameters are manditory.
+the Python version, but all parameters are manditory, additionally
+there as an argument that should be set to be true if you want to use
+long options with one dash or plus but not short options.
@code{args_arguments}, @code{args_arguments_count},
@code{args_unrecognised_count}, @code{args_message} and @code{args_files}
diff --git a/src/argparser.bash b/src/argparser.bash
index 3144f01..d8dc967 100644
--- a/src/argparser.bash
+++ b/src/argparser.bash
@@ -44,6 +44,7 @@ args_VARIADIC=2
# @param $3:str Long, multi-line, description of the program, empty for none
# @param $4:str The name of the program, empty for automatic
# @param $5:str Output channel, by fd
+# @param $6:int Set to 1 to use single dash/plus for long options
function args_init
{
test "$TERM" = linux
@@ -67,6 +68,7 @@ function args_init
fi
args_out="$(realpath "/proc/$$/fd/${args_out}")"
args_files=()
+ args_alternative="$6"
}
@@ -494,7 +496,7 @@ function args_parse
elif [ "$arg" = "--" ]; then
dashed=1
elif (( ${#arg} > 1 )) && [ "${_arg::1}" = "-" ]; then
- if (( ${#arg} > 2 )) && [ "${arg::1}" = "${arg:1:1}" ]; then
+ if [ "${args_alternative}" = 1 ] || (( ${#arg} > 2 )) && [ "${arg::1}" = "${arg:1:1}" ]; then
if [ ! $dontget = 0 ]; then
(( dontget-- ))
elif [ ! -e "${args_optmap}/${arg}" ]; then
diff --git a/src/argparser.c b/src/argparser.c
index 1b1c704..46b1509 100644
--- a/src/argparser.c
+++ b/src/argparser.c
@@ -46,6 +46,11 @@ static void** map_free(args_Map* map);
static long args_linuxvt;
/**
+ * Whether to use single dash/plus long options
+ */
+static long args_alternative;
+
+/**
* Whether to free the member of `args_program`
*/
static long args_program_dispose;
@@ -111,8 +116,9 @@ static long args_map_values_size;
* @param longdescription Long, multi-line, description of the program, may be `null`
* @param program The name of the program, `null` for automatic
* @param usestderr Whether to use stderr instead of stdout
+ * @param alternative Whether to use single dash/plus long options
*/
-void args_init(char* description, char* usage, char* longdescription, char* program, long usestderr)
+void args_init(char* description, char* usage, char* longdescription, char* program, long usestderr, long alternative)
{
char* term = getenv("TERM");
args_linuxvt = 0;
@@ -135,6 +141,7 @@ void args_init(char* description, char* usage, char* longdescription, char* prog
args_usage = usage;
args_longdescription = longdescription;
args_out = usestderr ? stderr : stdout;
+ args_alternative = alternative;
args_arguments_count = args_unrecognised_count = args_files_count = 0;
args_files = args_arguments = null;
args_message = null;
@@ -1200,7 +1207,7 @@ long args_parse(int argc, char** argv)
else if ((*arg == '-') && (*(arg + 1) == '-') && (*(arg + 2) == 0))
dashed = true;
else if (((*arg == '-') || (*arg == '+')) && (*(arg + 1) != 0))
- if (*arg == *(arg + 1))
+ if (args_alternative || (*arg == *(arg + 1)))
{
if (dontget > 0)
dontget--;
diff --git a/src/argparser.h b/src/argparser.h
index 580a68d..a3df727 100644
--- a/src/argparser.h
+++ b/src/argparser.h
@@ -168,14 +168,15 @@ long args_files_count;
* @param longdescription Long, multi-line, description of the program, may be `null`
* @param program The name of the program, `null` for automatic
* @param usestderr Whether to use stderr instead of stdout
+ * @param alternative Whether to use single dash/plus long options
*/
-extern void args_init(char* description, char* usage, char* longdscription, char* program, long usestderr);
+extern void args_init(char* description, char* usage, char* longdscription, char* program, long usestderr, long alternative);
/**
* Disposes of all resources, run this when you are done
*/
-extern void args_dispose();
+extern void args_dispose(void);
/**
@@ -213,14 +214,14 @@ extern args_Option args_new_variadic(char* argument, int standard, char* alterna
*
* @return All options
*/
-extern args_Option* args_get_options();
+extern args_Option* args_get_options(void);
/**
* Gets the number of elements in the array returned by `args_get_options`
*
* @return The number of elements in the array returned by `args_get_options`
*/
-extern long args_get_options_count();
+extern long args_get_options_count(void);
/**
* Gets the option with a specific index
@@ -284,14 +285,14 @@ extern char* args_options_get_help(long index);
*
* @return The available options
*/
-extern char** args_get_opts();
+extern char** args_get_opts(void);
/**
* Gets the number of available options
*
* @return The number of available options
*/
-extern long args_get_opts_count();
+extern long args_get_opts_count(void);
/**
* Gets whether an option is available
@@ -369,14 +370,14 @@ extern long args_opts_used(char* name);
*
* @return All alternativ names that exists for all options
*/
-extern char** args_get_optmap();
+extern char** args_get_optmap(void);
/**
* Gets the number of elements returned by `args_get_optmap`
*
* @return The number of elements returned by `args_get_optmap`
*/
-extern long args_get_optmap_count();
+extern long args_get_optmap_count(void);
/**
* Maps alternative name for a option
@@ -489,12 +490,12 @@ extern long args_test_exclusiveness(char** exclusives, long exclusives_count);
/**
* Maps up options that are alternatives to the first alternative for each option
*/
-extern void args_support_alternatives();
+extern void args_support_alternatives(void);
/**
* Prints a colourful help message
*/
-extern void args_help();
+extern void args_help(void);
/**
* Parse arguments
diff --git a/src/argparser.py b/src/argparser.py
index d9f9d55..93b0287 100644
--- a/src/argparser.py
+++ b/src/argparser.py
@@ -176,12 +176,13 @@ class ArgParser():
self.optmap[alt] = (stdalt, ArgParser.VARIADIC)
- def parse(self, argv = sys.argv):
+ def parse(self, argv = sys.argv, alternative = False):
'''
Parse arguments
- @param args:list<str> The command line arguments, should include the execute file at index 0, `sys.argv` is default
- @return :bool Whether no unrecognised option is used
+ @param args:list<str> The command line arguments, should include the execute file at index 0, `sys.argv` is default
+ @param alternative:bool Use single dash/plus for long options
+ @return :bool Whether no unrecognised option is used
'''
self.argcount = len(argv) - 1
self.arguments = argv[1:]
@@ -220,7 +221,7 @@ class ArgParser():
elif arg == '++': tmpdashed = True
elif arg == '--': dashed = True
elif (len(arg) > 1) and (arg[0] in ('-', '+')):
- if (len(arg) > 2) and (arg[:2] in ('--', '++')):
+ if alternative or ((len(arg) > 2) and (arg[:2] in ('--', '++'))):
if dontget > 0:
dontget -= 1
elif (arg in self.optmap) and (self.optmap[arg][1] == ArgParser.ARGUMENTLESS):
diff --git a/src/argparser/ArgParser.java b/src/argparser/ArgParser.java
index ddb09c5..a786f23 100644
--- a/src/argparser/ArgParser.java
+++ b/src/argparser/ArgParser.java
@@ -962,6 +962,19 @@ public class ArgParser
*/
public boolean parse(final String[] argv)
{
+ return parse(argv, false);
+ }
+
+
+ /**
+ * Parse arguments
+ *
+ * @param args The command line arguments, however it should not include the execute file at index 0
+ * @param alternative Use single dash/plus for long options
+ * @return Whether no unrecognised option is used
+ */
+ public boolean parse(final String[] argv, final boolean alternative)
+ {
this.arguments = argv;
final ArrayList<String> argqueue = new ArrayList<String>();
@@ -987,7 +1000,7 @@ public class ArgParser
else if (arg.equals("++")) tmpdashed = true;
else if (arg.equals("--")) dashed = true;
else if ((arg.length() > 1) && ((arg.charAt(0) == '-') || (arg.charAt(0) == '+')))
- if ((arg.length() > 2) && (arg.charAt(1) == arg.charAt(0)))
+ if (alternative || ((arg.length() > 2) && (arg.charAt(1) == arg.charAt(0))))
{ Option opt = this.optmap.get(arg);
if (dontget > 0)
dontget--;
diff --git a/src/test.c b/src/test.c
index 7d8ad32..41c9bce 100644
--- a/src/test.c
+++ b/src/test.c
@@ -41,7 +41,7 @@ int main(int argc, char** argv)
"GNU Affero General Public License for more details.\n"
"\n"
"You should have received a copy of the GNU Affero General Public License\n"
- "along with this library. If not, see <http://www.gnu.org/licenses/>.", 0, 1);
+ "along with this library. If not, see <http://www.gnu.org/licenses/>.", 0, 1, 0);
args_add_option(args_new_argumentless(0, "-h", "-?", "--help", NULL), "Prints this help message\n(and exits)");
args_add_option(args_new_argumentless(0, "--hello", NULL), "Prints the text: hello world'");