jswieca
jswieca

Reputation: 135

How to escape chars from string option

I have simple java app with one command and couple options. To one of the option I'd like to pass string with single quotes and spaces. Is it possible?

I use picocli 4.5.0. Below is what I'd like to achieve

java -jar my-app.jar my-command --arg-a=aaa --arg-b=bbb --arg-c="x=y and z=w(to_date(xxx, 'YYYYMMDD'))"

I set trimQuotes to false, but I get error

Unmatched arguments from index X: 'and' 'z=w(to_date(xxx, 'YYYYMMDD'))'

Is it possible to escape this whole string/option?

@Command(name = "process-data", description = "Bla bla bla...")
public class MyApp implements Callable<Integer> {
   @Option(
       names = {"--arg-a"},
       required = true
    )
    private String argA;

    @Option(
        names = {"--arg-b"},
        required = true
    )
    private String argB;

   @Option(
        names = {"--arg-c"},
        required = true
    )
    private String argC;

    @Override
    public Integer call() {
    ...
    }
}

public static void main(String[] args) {
    new CommandLine(MyApp.create()).execute(args);
}

Upvotes: 1

Views: 512

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 546025

No need for trimQuotes. The following works:

@Command(description = "nothing here", name = "my-command", version = "0.1")
public class MyCommand implements Callable<Void> {
    @Option(names = {"--arg-a"}, description = "A")
    private String a;

    @Option(names = {"--arg-b"}, description = "B")
    private String b;

    @Option(names = {"--arg-c"}, description = "C")
    private String c;

    public static void main(final String[] args) {
        System.exit(new CommandLine(new MyCommand()).execute(args));
    }

    @Override
    public Void call() throws Exception {
        System.err.printf("a = %s\nb = %s\nc = %s\n", a, b, c);
        return null;
    }
}
⟩⟩⟩ java MyCommand --arg-a=aaa --arg-b=bbb --arg-c="x=y and z=w(to_date(xxx, 'YYYYMMDD'))"
a = aaa
b = bbb
c = x=y and z=w(to_date(xxx, 'YYYYMMDD'))

Upvotes: 1

Related Questions