Convenient mechanism to get key/value pairs for command line args

23 views
Skip to first unread message

Joseph Madden

unread,
Apr 29, 2016, 11:31:32 AM4/29/16
to jopt-simple
Hi All,

I'm writing a simple app and I'd like to log all values associated with all options (provided or defaulted).  So far, I have something like the following:

OptionParser OPTION_PARSER = new OptionParser();

#...add some specs

OptionSet optionSet = OPTION_PARSER.parse(args);

OPTION_PARSER
.recognizedOptions().forEach((t, u) -> LOGGER.info("[{}] == {}", t, optionSet.valueOf(u)));


This produces duplicates for the options which have aliases.  Is there a better way to do this?

Any help appreciated.

Regards,

Joe

Paul Holser

unread,
Jun 2, 2016, 11:37:54 AM6/2/16
to jopt-simple
Hi Joe, thanks for your interest, and my apologies for the late reply.

One option (no pun intended) would be to make OptionParser.recognizedOptions() return only one entry for an option that has synonyms.

Would it matter to you which synonym gets to be "recognized"? Would the first synonym specified be enough?

Let's discuss in a GitHub issue. Would you mind to create one?

Thanks,
p

Paul Holser

unread,
Jun 2, 2016, 11:45:36 AM6/2/16
to jopt-simple
Hi Joe,

May I also suggest OptionSet.asMap()?

    optionSet.asMap().forEach((o, args) -> Logger.info("{} == {}", o, args));

Let us know how this works for you.

Cheers,
p


Joseph Madden

unread,
Jun 2, 2016, 12:24:12 PM6/2/16
to jopt-simple
Hi Paul,

Thanks for your reply.  Your suggestion works perfectly for my use case.

Cheers,

Joe
Reply all
Reply to author
Forward
0 new messages