[ 
https://issues.apache.org/jira/browse/IVY-480?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12491722
 ] 

Archie Cobbs commented on IVY-480:
----------------------------------

The "--" option terminates option processing.

Example: compile this program:

{noformat}
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;

class xx {

    private static Options getOptions() {
        Option conf = OptionBuilder.withArgName( "conf" )
            .hasArg()
            .withDescription(  "specify configuration" )
            .create( "conf" );
        Options options = new Options();
        options.addOption(conf);
        return options;
    }

    public static void main(String[] args) throws Exception {
        Options options = getOptions();
        GnuParser parser = new GnuParser();
        CommandLine line = parser.parse( options, args );
        String[] extra = line.getArgs();
        System.out.println("there are " + extra.length + " extra args:");
        for (int i = 0; i < extra.length; i++)
            System.out.println("["+i+"] = \"" + extra[i] + "\"");
    }
}
{noformat}

Now run it:

{noformat}
$ java xx -conf asdf -- foo -conf bar
there are 3 extra args:
[0] = "foo"
[1] = "-conf"
[2] = "bar"
{noformat}



> Allow "main" parameters to be passed directly (instead of using -args flag)
> ---------------------------------------------------------------------------
>
>                 Key: IVY-480
>                 URL: https://issues.apache.org/jira/browse/IVY-480
>             Project: Ivy
>          Issue Type: Improvement
>          Components: Core
>    Affects Versions: 1.4.1
>            Reporter: Archie Cobbs
>         Attachments: argspatch.txt
>
>
> When running ivy in standalone mode using the {{-main}} flag, any extra 
> parameters passed on the command line (i.e., not as flag arguments) are 
> simply ignored.
> E.g., if I invoke {{java fr.jayasoft.ivy.Main -main com.exmaple.Main foo 
> bar}} the {{foo bar}} parameters are ignored. To pass "foo" and "bar" to my 
> main method, I have to use {{-args foo}} and {{-args bar}}.
> Suggestion: when using the {{-main}} flag, allow the parameters to the main 
> method to be passed directly on the command line as "extra" parameters, as an 
> alternative to using the {{-args}} flag.
> The motivation for this is that this would be much easier to deal with in 
> shell scripts. E.g. suppose I want to write a shell script {{/usr/bin/foo}} 
> which uses ivy to resolve dependencies and execute some main class 
> {{com.example.foo.Main}}. To do this I'd have to parse the command line into 
> individual arguments, then precatenate each one with a {{-args}} flag, etc. 
> This sounds hard enough, but when you consider that some parameters may be 
> quoted, it becomes nearly impossible. With the change suggested here, it 
> would become trivial: I'd just do something like {{ivy -ivy 
> /usr/share/foo.ivy -main com.example.foo.Main ${1+"$@"} }} in the shell 
> script.
> If this is implemented, it should probably become the recommended approach, 
> and {{-args}} be deprecated.
> As a side note, the {{-args}} flag is mis-named: it should be called {{-arg}} 
> instead (but I'd prefer to have it just be deprecated).

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to