There is such a startup line:

java -jar smart-file-handler-cli -p -s xml \ -o /path/result.log 'text for search' ./ 

Question about application arguments:

 -p -s xml -o /path/result.log 'text for search' ./ 

How is it most convenient to parse? Given that after -o , this is all the same value of an optional argument.

I tried to do regex :

 /((-[\w]) (.*)?) ((-[\w]) (.*)?)/g 

I tried to use command parsers, such as: commons cli, google options. But they did not adequately spars the -o option.

Can modify the routine? Or how else can you?

  • one
    ready-made solutions are best used, for example, Apache Common CLI commons.apache.org/proper/commons-cli - DaysLikeThis
  • @DaysLikeThis I kind of described how he doesn't parse the whole team. - Tsyklop
  • option.getValues() option to get the value for -o on my side as option.getValues() and then compile a single string from this array is acceptable? Although, this is a crutch, most likely - Chubatiy
  • @Chubatiy the problem is that he does not write down everything after the path at all. I mean, this part of the 'text for search' ./ stupidly disappears. - Tsyklop pm
  • @Tsyklop if the parameter includes delimiters, you need to combine them with quotation marks -o "/path/result.log 'text for search'" - DaysLikeThis

1 answer 1

  Option option1 = Option .builder() .longOpt("p") .argName("p") .build(); Option option2 = Option .builder() .longOpt("s") .argName("s") .hasArgs() .build(); Option option3 = Option .builder() .longOpt("o") .argName("o") .hasArgs() .numberOfArgs(3) .build(); CommandLineParser parser = new DefaultParser(); CommandLine parse = parser.parse(new Options().addOption(option1).addOption(option2).addOption(option3), args); for (Option o : parse.getOptions()) { System.out.println(o.getLongOpt() + " " + Arrays.toString(o.getValues())); } 

Those. then glue o.getValues() for your case

Input arguments

-p -s xml -o /path/result.log 'text for search' ./

The output of the program

p null s [xml] o [/path/result.log, text for search, ./]

  • And what array of arguments? - Tsyklop
  • @Tsyklop program output cited as an example - Chubatiy
  • And initially what array of arguments? Just what I gave in the example in the first post, these are conditions. I can not change them. - Tsyklop
  • @Tsyklop as you wrote in the question. But I gave an example of a launch - Chubatiy
  • This is weird. I only have /path/result.log in -o - Tsyklop