I'm writing a shell in Java because I haven't programmed in Java for far too long and I've forgotten a lot of it. I was going to write my own, but right now I'd like to get a working prototype, so I would like to drop in an existing command-line option parser.
Requirements:
- Takes an arbitrary
String
orString[]
(i.e. doesn't use the arguments from the command line) - Takes arguments in a similar format to
getopt
(the GNU enhanced version); specifically:- Multiple single-letter options can be joined (e.g.
-a -b -c
to-abc
) - Supports long options (e.g.
--message="Hello!"
) (though it doesn't have to support single-dash long options) - Assumes all non-option-like bits at the end are parameters to be passed normally (e.g.
-abc --long="Hello!" param1 param2
tells me that the parameters areparam1
andparam2
) --
can be used to separate options from arguments (e.g.-ab --custom="hello" -- -file_starting_with_hyphen -another
gives me the options/flagsa
,b
, andcustom
with the appropriate values, and tells me that the arguments are-file_starting_with_hyphen
and-another
)- Whitespace can be part of arguments, if it's quoted.
- An option name followed by a value is parsed as the option having that value (e.g.
-h foo
says that there is an option,h
, with a value,foo
)
- Multiple single-letter options can be joined (e.g.
- Entirely cross-platform
- Doesn't need me to specify which options I'm looking for (i.e. I pass it a
String
orString[]
and it tells me which flags/options were set, as opposed to it looking for the options I want to set and assuming the rest are arguments)- This means it doesn't use annotations to specify where to store the flag values. At least two answers so far have done this.
- Free (as in beer)
- Can be legally used in any project (i.e. not noncommercial, not GPL)
Ideal, but not necessary:
- Small -- one file
- Minimal copyright license (Not copyleft -- I like not worrying about legal issues, and I hate people trying to tell me that I can't use my work, however, I damned well, please)
- Uses the built-in interfaces (
java.util.Map
, specifically) to return data. (This is so I can write my own function more easily later) - Open source
works like getopt
" is your second requirement.