From 92025a894b9357e75c7cb06164cd4f61185f1751 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 18 Jun 2013 15:51:23 +0200 Subject: misc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/ArgParser.java | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/argparser.py | 14 ++++++------- 2 files changed, 68 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/ArgParser.java b/src/ArgParser.java index 6ea7e65..e75ba91 100644 --- a/src/ArgParser.java +++ b/src/ArgParser.java @@ -163,6 +163,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 */ @@ -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 -- cgit v1.2.3-70-g09d2