Help with using Commands with Verb + Noun syntax

57 views
Skip to first unread message

to...@thelastpickle.com

unread,
Aug 9, 2017, 6:38:50 PM8/9/17
to jcommander
Hey all,
  I'm building an operational CLI utility for our software, and I can't seem to quite find an example that meets my needs.  As a team we're familiar with kubectl of kubernetes, this has the nice consistent command line format of

binary verb noun (possible other options), flags

For instance, I would like to implement the following commands.

sandwich get bundles (bundle options, time range etc)


sandwich reprocess bundles (bundle options, time range etc)


Another set of commands might be something like this.

sandwich reindex bundles

sandwich reindex facts



Each one of the verb + nouns make up a command.   I found this documentation, http://jcommander.org/#_more_complex_syntaxes_commands, but it's not quite clear to me how to accomplish this, since it's really a specific path through a tree of inputs to get to my command.  Any help would be greatly appreciated!

Thanks,
Todd

Cédric Beust ♔

unread,
Aug 9, 2017, 6:40:16 PM8/9/17
to jcommander

Hi Todd,

I think what you are looking for is variable arities.


-- 
Cédric


--
You received this message because you are subscribed to the Google Groups "jcommander" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

to...@thelastpickle.com

unread,
Aug 9, 2017, 7:11:10 PM8/9/17
to jcommander, ced...@beust.com
Hi Cédric,

Thanks for the response.

I looked into those, but they seem to require a flag before the parameters.  The examples have this text.

program -foo a1 a2 a3 -bar
program
-foo a1 -bar

Is this not the case?  

I would prefer to avoid the flags.

Thanks,
Todd

-- 
Cédric


To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+...@googlegroups.com.

Cédric Beust ♔

unread,
Aug 9, 2017, 7:12:54 PM8/9/17
to jcommander
Yes, that option would be the verb.

-- 
Cédric


To unsubscribe from this group and stop receiving emails from it, send an email to jcommander+unsubscribe@googlegroups.com.

to...@thelastpickle.com

unread,
Aug 9, 2017, 8:43:41 PM8/9/17
to jcommander
Hi Cedric,
  I'm not sure I follow, lets say I have 2 command classes.  GetBucketCommand and QueueBucketCommand.  How would I go about specifying they're required by verb noun?  I have the following code.



/**
 * This class is responsible for listing all bundles and printing them to stdout
 */

@Parameters(commandDescription = "Lists all bundle files in the specified resources")
public final class GetBundlesCommand implements Command
{


   
@Parameter(names = "bucket", description = "The S3 bucket to list resources from")
   
String s3Bucket;


   
@Parameter(names = "region", description = "The region of the s3 bucket")
   
String s3Region;
}



/**
 * This class is responsible for getting all bundles, and re-queueing them.  Will re-use GetBundlesCommand
 * to ensure compatibility across list + process functionality
 */

@Parameters(commandDescription = "Queues all bundle files in the specified resources")
public final class QueueBundlesCommand
{


   
@Parameter(names = "dry-run", description = "True if the queue should not be executed")
   
private boolean dryRun;


   
}


Adding them in the following way doesn't work, because this expects a single string in the index, not 2.  



/**
 * The main class that is invoked when the program starts
 */

public final class Main
{
   
public static void main(final String[] args)
   
{
       
final GetBundlesCommand getBundlesCommand = new GetBundlesCommand();
       
final QueueBundlesCommand queueBundlesCommand = new QueueBundlesCommand();


       
final JCommander jc = JCommander.newBuilder()
               
.addCommand("get bundles", getBundlesCommand)
               
.addCommand("queue bundles", queueBundlesCommand)
               
.build();


        jc
.parse(args);


   
}
}

I somewhat understand what you're saying, but it's not clear to me how to wire this up.  I'd be happy to contribute a PR to the documentation once I have it working.   Any help would be greatly appreciated.

Thanks,
Todd

Dominik Kaspar

unread,
Oct 2, 2017, 9:29:45 AM10/2/17
to jcommander
I'm looking exactly for the same thing. I guess the only way to do this right now is to use, for example, an underscore for separating the noun and verb.

        final JCommander jc = JCommander.newBuilder()

                
.addCommand("get_bundles", getBundlesCommand)
                
.addCommand("queue_bundles", queueBundlesCommand)
                
.build();

dan.die...@gmail.com

unread,
Jan 30, 2018, 10:06:13 AM1/30/18
to jcommander
I ran into this also and came up with the same option: the command can't have spaces in it. I used hyphens.
Reply all
Reply to author
Forward
0 new messages