diff options
author | Mattias Andrée <maandree@operamail.com> | 2013-06-18 15:51:23 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2013-06-18 15:51:23 +0200 |
commit | 92025a894b9357e75c7cb06164cd4f61185f1751 (patch) | |
tree | 94f15085f2e30afbf66a65f3abf432e9eddd421d /src | |
parent | implement help for bash version (diff) | |
download | argparser-92025a894b9357e75c7cb06164cd4f61185f1751.tar.gz argparser-92025a894b9357e75c7cb06164cd4f61185f1751.tar.bz2 argparser-92025a894b9357e75c7cb06164cd4f61185f1751.tar.xz |
misc
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/ArgParser.java | 61 | ||||
-rw-r--r-- | src/argparser.py | 14 |
2 files changed, 68 insertions, 7 deletions
diff --git a/src/ArgParser.java b/src/ArgParser.java index 6ea7e65..e75ba91 100644 --- a/src/ArgParser.java +++ b/src/ArgParser.java @@ -164,6 +164,11 @@ public class ArgParser public int unrecognisedCount = 0; /** + * The concatination of {@link #files} with blankspaces as delimiters, {@code null} if no files + */ + public String message = null; + + /** * Options, in order */ private final ArrayList<Option> options = new ArrayList<Option>(); @@ -1002,6 +1007,62 @@ public class ArgParser this.files.add(arg); } + int i = 0, n = optqueue.size(); + while (i < n) + { + final String opt = this.optmap.get(optqueue.get(i)).standard; + final String arg = argqueue.size() > i ? argqueue.get(i) : null; + i++; + if (this.opts.get(opt) == null) + this.opts.put(opt, new String[] {}); + if (argqueue.size() >= i) + this.opts.put(opt, append(this.opts.get(opt), arg)); + } + + for (final Option opt : this.options) + if (opt.getClass() == Variadic.class) + { final String[] varopt = this.opts.get(opt.standard); + if (varopt != null) + { + final String[] additional = new String[this.files.size()]; + this.files.toArray(additional); + if (varopt[0] == null) + this.opts.put(opt.standard, additional); + else + this.opts.put(opt.standard, append(varopt, additional)); + this.files.clear(); + break; + } } + + final StringBuilder sb = new StringBuilder(); + for (final String file : this.files) + { sb.append(' '); + sb.append(file); + } + this.message = sb.toString().substring(1); + + if (this.unrecognisedCount > 5) + { int more = this.unrecognisedCount - 5; + this.print(this.program + ": warning: " + more + " more unrecognised "); + this.println(more == 1 ? "option" : "options"); + } + + return rc; + } + + + /** + * Create a new identical array, except with extra items at the end + * + * @param array The array + * @param items The new items + * @return The new array + */ + private String[] append(final String[] array, final String... items) + { + final String[] rc = new String[array.length + items.length]; + System.arraycopy(array, 0, rc, 0, array.length); + System.arraycopy(items, 0, rc, array.length, items.length); return rc; } diff --git a/src/argparser.py b/src/argparser.py index 18a8899..896af10 100644 --- a/src/argparser.py +++ b/src/argparser.py @@ -137,8 +137,8 @@ class ArgParser(): @parma default:str|int The default argument's name or index @param help:str? Short description, use `None` to hide the option ''' - self.__options.append((ArgParser.ARGUMENTLESS, alternatives, None, help)) stdalt = alternatives[default] if isinstance(default, int) else default + self.__options.append((ArgParser.ARGUMENTLESS, alternatives, None, help, stdalt)) self.opts[stdalt] = None for alt in alternatives: self.optmap[alt] = (stdalt, ArgParser.ARGUMENTLESS) @@ -153,8 +153,8 @@ class ArgParser(): @param arg:str The name of the takes argument, one word @param help:str? Short description, use `None` to hide the option ''' - self.__options.append((ArgParser.ARGUMENTED, alternatives, arg, help)) stdalt = alternatives[default] if isinstance(default, int) else default + self.__options.append((ArgParser.ARGUMENTED, alternatives, arg, help, stdalt)) self.opts[stdalt] = None for alt in alternatives: self.optmap[alt] = (stdalt, ArgParser.ARGUMENTED) @@ -169,8 +169,8 @@ class ArgParser(): @param arg:str The name of the takes arguments, one word @param help:str? Short description, use `None` to hide the option ''' - self.__options.append((ArgParser.VARIADIC, alternatives, arg, help)) stdalt = alternatives[default] if isinstance(default, int) else default + self.__options.append((ArgParser.VARIADIC, alternatives, arg, help, stdalt)) self.opts[stdalt] = None for alt in alternatives: self.optmap[alt] = (stdalt, ArgParser.VARIADIC) @@ -287,20 +287,20 @@ class ArgParser(): for arg in self.__options: if arg[0] == ArgParser.VARIADIC: - varopt = self.opts[arg[1][0]] + varopt = self.opts[arg[1][4]] if varopt is not None: additional = ','.join(self.files).split(',') if len(self.files) > 0 else [] if varopt[0] is None: - self.opts[arg[1][0]] = additional + self.opts[arg[1][4]] = additional else: - self.opts[arg[1][0]] = varopt[0].split(',') + additional + self.opts[arg[1][4]] = varopt[0].split(',') + additional self.files = [] break self.message = ' '.join(self.files) if len(self.files) > 0 else None if self.unrecognisedCount > 5: - sys.stderr.write('%s: warning: %i more unrecognised %s\n' % (self.unrecognisedCount - 5, 'options' if self.unrecognisedCount == 6 else 'options')) + self.__print('%s: warning: %i more unrecognised %s\n' % (self.program, self.unrecognisedCount - 5, 'options' if self.unrecognisedCount == 6 else 'options')) return self.rc |