Getting 'the rest' from first non recognised value, and calculatable (based on args) arity

21 views
Skip to first unread message

Turloch O'Tierney

unread,
May 24, 2017, 12:19:47 PM5/24/17
to jcommander
Hi,

I am looking to maintain/parse/update the arguments to an existing application (to implement similar functionality in Java).
 
I want to parse:
mycommand -flagone arg1 arg2_maynot_exist_for_some_arg1 something_hard_to_match_leftover @filereference.sql arbitraryarg1 -flagone

into:
-flagone arg1 arg2_maynot_exist_for_some_arg1

@filereference.sql arbitraryarg1 -flagone

and left over:
something_hard_to_match_leftover

i.e.
[1]@... option starting with @ to the end including argument that may match a flag (or just dump from first non match ie: something_hard_to_match_leftover @filereference.sql arbitraryarg1 -flagone
just in case the rest happens to match a flag like -flagone)

[2] -flagone having semi fixed arity (depending on subsequent args) (but not all the way to next flag).

Looks to me like:
something_hard_to_match_leftover @filereference.sql arbitraryarg1 -flagone
will be custom enough (and cannot be avoided) jcommander would be useful for automating simpler more well defined options (and printing usage on syntax error).

Started to look at the jcommander code to get a better grasp of it.

Thank you for your time.

Turloch O'Tierney

The views expressed in this post are my own and do not necessarily reflect the views of (my employer) Oracle Corporation.
http://totierne.blogspot.co.uk/

Notes
[1]I want to use variable arity something like:

the_command -flag_arity0 something_hard_to_match_leftover @any_thing the rest even with -flag_arity0

ie @any_thing has arity 'to the end' i.e. 'the rest even with -flag_arity0'

Did not pop out at me.
(Option: I preprocess the '@' requirement and pass the rest to jcommander - and hope no flag/arity args begin with @)
Note any_thing might be anything actually a file name reference, dynamic parameter does not look good as they do not have arity


[2]-myflag arg1 arg2_maynot_exist_for_some_arg1 something_hard_to_match_leftover
I am have just started to look at commands for semi structured arity -
options:
1/I might be looking for subcommands which have their own arity,
2/I may be looking for a filter (that I write)  that knows when it is finished (or throws a recoverable exception when 'gone too far').

awkward to get 'to next flag' in case arg equals a flag,
awkward to take the rest as something_hard_to_match_leftover will be eaten as part of the -myflag.


Turloch O'Tierney

unread,
May 31, 2017, 8:04:30 AM5/31/17
to jcommander
Hi,

Looking at the code: (Changing/forking the code is the worst case scenario for maintenance reasons).

1/Rest after first unknown isOption(String)==false : Looks like would require extra flag (or extend/open up privileges)
2/VariableArity - OK in at least worst/raw case 'java on string[]'

1/Rest after first unknown isOption(String)==false: Looks like would require extra flag (or extend/open up privileges)
ie after first non option - arguments that happen to be match the text of option flags should not be  recognised as options.
Could do via: configuration flag or extending JCommander and making some arguments protected rather than private. (Note JCommander is instantiated through builder).
   protected boolean isOption(String passedArg) {
        if ((unknownArgs!=null)&&(unknownArgs.isEmpty()==false)) {
            return (false);
        } else {
            return super.isOption(passedArg);
        }
    }

2/VariableArity: - OK in at least worst/raw case 'java on string[]'
(jcommander commands may be easier programmed elsewhere (via jcommander commands etc) ie not raw java on String[])
Looks like this is programmable - overall parameter class can  "implements IVariableArity" (rather than individual parameter via annotation).

Standard variable arity is until next option (or end of string[]) - or through (nested?) commands for varity depends on (sub) arguments.

 
Thank you for your time.

Turloch O'Tierney

The views expressed in this post are my own and do not necessarily reflect the views of (my employer) Oracle Corporation.
http://totierne.blogspot.co.uk/

Reply all
Reply to author
Forward
0 new messages