Menu

2nd argument depending on first one

Help
2009-03-17
2013-05-30
  • Gilles Rayrat

    Gilles Rayrat - 2009-03-17

    Hi all,
    I wanted to do the following: a first completor would allow entries "a" "b" and "c".
    Then, if the first argument is "a", allow "1" "2" and "3". If the first arg is "b", allow "4", "5". "6"
    etc.

    Also I noticed that the 2nd example of ArgumentCompletor (in javadoc) does not do what it says:
    consoleReader.addCompletor (new ArgumentCompletor (
                    new SimpleCompletor (new String [] { "foo", "bar", "baz"})));
            consoleReader.addCompletor (new ArgumentCompletor (
                    new FileNameCompletor ()));
    will result in always proposing foo bar and baz, never the filenames

    Thanks!
    Gilles.

     
    • Gilles Rayrat

      Gilles Rayrat - 2009-03-18

      Answer to my own question:

              List argCompletor1= new LinkedList();
              argCompletor1.add(new SimpleCompletor(new String[] { "a", "b" }));
              argCompletor1.add(new SimpleCompletor(new String[] { "1", "2" }));
              List argCompletor2= new LinkedList();
              argCompletor2.add(new SimpleCompletor(new String[] { "x", "y" }));
              argCompletor2.add(new SimpleCompletor(new String[] { "3", "4" }));
              List comps = new LinkedList();
              comps.add(new ArgumentCompletor(argCompletor1));
              comps.add(new ArgumentCompletor(argCompletor2));
             
              reader.addCompletor(new MultiCompletor(comps));

      Many thanks to Mariusz Gniazdowski for his blog article http://emg-2.blogspot.com/2008/03/comfortable-command-line-in-java.html

       
  • Anonymous

    Anonymous - 2009-10-30

    I need to do the same thing.

    Did you figure out how ?

    Thanks,

    Leonid

     
  • Anonymous

    Anonymous - 2009-10-30

    Please ignore my previous message. I did not see your own reply when I send it.

     
  • Anonymous

    Anonymous - 2009-10-30

    Gilles,

    Actually I tried your suggestion now and it does not work as expected.
    Below is a fragment of my test program. When I enter "launch" and then enter "p" and press TAB, it offers me both "potato" and "plane". I expected that only "plane" will be offered.

    Any ideas?

    By the way, I did not quite get the point of your example. All your arguments are just one character long, what the completors are supposed to complete?

            List completors1 = new LinkedList();
            completors1.add(new SimpleCompletor(new String{"launch"}));
            completors1.add(new SimpleCompletor(new String{"plane", "rocket"}));

            List completors2 = new LinkedList();
            completors2.add(new SimpleCompletor(new String{"eat"}));
            completors2.add(new SimpleCompletor(new String{"potato", "apple"}));
            ArgumentCompletor ac1 = new ArgumentCompletor(completors1);
            ac1.setStrict(false);
            ArgumentCompletor ac2 = new ArgumentCompletor(completors2);
            ac2.setStrict(false);

            List completors = new LinkedList();
            completors.add(ac1);
            completors.add(ac2);

            reader.addCompletor(new MultiCompletor(completors));

     
  • Anonymous

    Anonymous - 2009-10-30

    I figured out why it is working that way: because I set strict to false.
    I removed that, and now it offers proper choices.

    However, I do need to set the strict to false for other reason: I have to allow the user to type other things manually that will not be matched.

    Basically, I need to support a list of commands, where each command has its own list of parameters, and those parameters are assigned values by the user. Something like this:

    getpositions accountId=123 instrumentId=456

    So I want jline to complete the command names and parameter names, but the values the user has to enter. With the strict mode, completor stops working after the first value is entered.

    What is the solution?

    Thanks,

    Leonid

     

Log in to post a comment.