private boolean matchArg(String arg, IKey key) { String kn = options.caseSensitiveOptions ? key.getName() : key.getName().toLowerCase(); if (options.allowAbbreviatedOptions) { if (kn.startsWith(arg)) return true; } else { ParameterDescription pd = descriptions.get(key); if (pd != null) { // It's an option. If the option has a separator (e.g. -author==foo) then // we only do a beginsWith match String separator = getSeparatorFor(arg); if (! " ".equals(separator)) { if (arg.startsWith(kn)) return true; } else { if (kn.equals(arg)) return true; } } else { // It's a command do a strict equality check if (kn.equals(arg)) return true; } } return false; }
private ParameterDescription getPrefixDescriptionFor(String arg) { for (Map.Entry<IKey, ParameterDescription> es : descriptions.entrySet()) { if (arg.startsWith(es.getKey().getName())) return es.getValue(); } return null; }
/** * Add a command object and its aliases. */ public void addCommand(String name, Object object, String... aliases) { JCommander jc = new JCommander(options); jc.addObject(object); jc.createDescriptions(); jc.setProgramName(name, aliases); ProgramName progName = jc.programName; commands.put(progName, jc); /* * Register aliases */ //register command name as an alias of itself for reverse lookup //Note: Name clash check is intentionally omitted to resemble the // original behaviour of clashing commands. // Aliases are, however, are strictly checked for name clashes. aliasMap.put(new StringKey(name), progName); for (String a : aliases) { IKey alias = new StringKey(a); //omit pointless aliases to avoid name clash exception if (!alias.equals(name)) { ProgramName mappedName = aliasMap.get(alias); if (mappedName != null && !mappedName.equals(progName)) { throw new ParameterException("Cannot set alias " + alias + " for " + name + " command because it has already been defined for " + mappedName.name + " command"); } aliasMap.put(alias, progName); } } }
private ParameterDescription getPrefixDescriptionFor(String arg) { for (Map.Entry<IKey, ParameterDescription> es : m_descriptions.entrySet()) { if (arg.startsWith(es.getKey().getName())) return es.getValue(); } return null; }
/** * Add a command object and its aliases. */ public void addCommand(String name, Object object, String... aliases) { JCommander jc = new JCommander(object); jc.setProgramName(name, aliases); jc.setDefaultProvider(m_defaultProvider); ProgramName progName = jc.m_programName; m_commands.put(progName, jc); /* * Register aliases */ //register command name as an alias of itself for reverse lookup //Note: Name clash check is intentionally omitted to resemble the // original behaviour of clashing commands. // Aliases are, however, are strictly checked for name clashes. aliasMap.put(new StringKey(name), progName); for (String a : aliases) { IKey alias = new StringKey(a); //omit pointless aliases to avoid name clash exception if (!alias.equals(name)) { ProgramName mappedName = aliasMap.get(alias); if (mappedName != null && !mappedName.equals(progName)) { throw new ParameterException("Cannot set alias " + alias + " for " + name + " command because it has already been defined for " + mappedName.m_name + " command"); } aliasMap.put(alias, progName); } } }
/** * Add a command object and its aliases. */ public void addCommand(String name, Object object, String... aliases) { JCommander jc = new JCommander(object); jc.setProgramName(name, aliases); jc.setDefaultProvider(m_defaultProvider); jc.setAcceptUnknownOptions(m_acceptUnknownOptions); ProgramName progName = jc.m_programName; m_commands.put(progName, jc); /* * Register aliases */ //register command name as an alias of itself for reverse lookup //Note: Name clash check is intentionally omitted to resemble the // original behaviour of clashing commands. // Aliases are, however, are strictly checked for name clashes. aliasMap.put(new StringKey(name), progName); for (String a : aliases) { IKey alias = new StringKey(a); //omit pointless aliases to avoid name clash exception if (!alias.equals(name)) { ProgramName mappedName = aliasMap.get(alias); if (mappedName != null && !mappedName.equals(progName)) { throw new ParameterException("Cannot set alias " + alias + " for " + name + " command because it has already been defined for " + mappedName.m_name + " command"); } aliasMap.put(alias, progName); } } }