public ArgParser(String[] args)
{
- // make a mutable new ArrayList so that shell globbing parser works
+ // Make a mutable new ArrayList so that shell globbing parser works.
+ // (When shell file globbing is used, there are a sequence of non-Arg
+ // arguments (which are the expanded globbed filenames) that need to be
+ // consumed by the --open/--argfile/etc Arg which is most easily done by
+ // removing these filenames from the list one at a time. This can't be done
+ // with an ArrayList made with only Arrays.asList(String[] args). )
this(new ArrayList<>(Arrays.asList(args)));
}
public ArgParser(List<String> args)
{
- init(args);
+ parse(args);
}
- private void init(List<String> args)
+ private void parse(List<String> args)
{
- // Enumeration<String> argE = Collections.enumeration(args);
int argIndex = 0;
- // while (argE.hasMoreElements())
+ boolean initialFilenameArgs = true;
for (int i = 0; i < args.size(); i++)
{
- // String arg = argE.nextElement();
String arg = args.get(i);
+
+ // If the first arguments do not start with "--" or "-" or is "open" and
+ // is a filename that exists it is probably a file/list of files to open
+ // so we fake an Arg.OPEN argument and when adding files only add the
+ // single arg[i] and increment the defaultLinkedIdCounter so that each of
+ // these files is opened separately.
+ if (initialFilenameArgs && !arg.startsWith(DOUBLEDASH)
+ && !arg.startsWith("-") && new File(arg).exists())
+ {
+ arg = DOUBLEDASH + Arg.OPEN.getName();
+ Console.debug("Adding argument '" + args.get(i)
+ + "' as a file to be opened");
+ }
+ else
+ {
+ initialFilenameArgs = false;
+ }
+
String argName = null;
String val = null;
List<String> vals = null; // for Opt.GLOB only
// will be used later. SubVals can be used.
if (a.hasOption(Opt.GLOB))
{
- vals.addAll(getShellGlobbedFilenameValues(a, args, i + 1));
+ // if this is the first argument with a file list at the start of
+ // the args we add filenames from index i instead of i+1
+ // and assume they should be opened separately
+ if (initialFilenameArgs)
+ {
+ val = args.get(i);
+ defaultLinkedIdCounter++;
+ }
+ else
+ {
+ vals = getShellGlobbedFilenameValues(a, args, i + 1);
+ }
}
else
{
{
avs = new ArgValues(a);
}
+
+ boolean argIndexIncremented = false;
// store appropriate value
if (a.hasOption(Opt.STRING))
{
for (String v : vals)
{
avs.addValue(makeSubstitutions(v), argIndex++);
+ argIndexIncremented = true;
}
}
else
avs.setBoolean(true, argIndex);
}
avs.incrementCount();
+ if (!argIndexIncremented)
+ argIndex++;
// store in appropriate place
if (a.hasOption(Opt.LINKED))