How to get value of command line arg into resource class?

1,356 views
Skip to first unread message

John Chesher

unread,
May 16, 2012, 1:21:46 PM5/16/12
to dropwiz...@googlegroups.com
I'd like to be able to pass a command line argument to my application and be able to access that value from the "resource" class.  I plan to use this to set the hostname:port for which database server I want it to connect to. Any way to do that or "best practices" for doing this?
I've seen this issue:  https://github.com/codahale/dropwizard/issues/40, which relates to overriding certain properties, with the example given of "-Ddw.database.url=jdbc:postgres://yay/whee", but I can not figure out
1) in what file does that property reside (the one being overridden)?
2) how to access it from my "resource" class?

Ideas?  Thanks!

Christopher Currie

unread,
May 16, 2012, 1:29:59 PM5/16/12
to dropwiz...@googlegroups.com
These command line parameters are part of the Configuration system. The Configuration object tree for your service implements a virtual "path" of command-line values, e.g. if your Configuration object has string property "foo", you can set it using "-Ddw.foo=value", but if it has an object property "foo", and that object has an int property "bar", you can set it using "-Ddw.foo.bar=1".

John Chesher

unread,
May 16, 2012, 1:33:43 PM5/16/12
to dropwiz...@googlegroups.com
And I should say that I thought about putting a property in the yaml file and adding it to the Configuration class, but the yaml file gets bundled into the deployed jar file.  I guess there are ways to edit the file within the jar, but I was hoping for an approach where I could just override the parameter in the command line, so the contents of the jar file do not have to be touched.

Tatu Saloranta

unread,
May 16, 2012, 3:37:37 PM5/16/12
to dropwiz...@googlegroups.com
I don't bundle my config files in the jar; there is no requirement to
do that. Why don't you just leave it out? (it does get included if you
store it under Maven source directories)

-+ Tatu +-
Message has been deleted

Robert J Antonucci

unread,
Sep 14, 2015, 11:49:14 AM9/14/15
to dropwizard-user
These command line parameters are part of the Configuration system... you can set it using "-Ddw.foo=value", 

For those (like me) reading this thread much later, you don't need to use the -Ddw.foo=bar format if you don't want to.  You can just grab strings off the command line like a regular java app, but your command class has to override the configure() method.  So let's say you want to be able to pass in "bar" as a command line argument.
e.g. java -jar myDropWizardApp.jar mycommand bar

Your command class would need to look like this:

public class MyCommand extends Command<MyConfiguration> {

public MyCommand() {
super("mycommand", "Read a string off the command line");
}
public void configure(Subparser argParser) {
argParser.addArgument("foo").nargs("?").help("Optional command line string.");
}

@Override
protected void run(Bootstrap<ViolationsConfiguration> bootstrap,
Namespace namespace, ViolationsConfiguration configuration)
throws Exception {
String fooValue= namespace.getString("foo");
...
}
}

Variations:
If your command is a ConfiguredCommand you need to add
super.configure(argParser);
as the first line of your configure() method or the YML file won't be read.

If you want a non-optional string try
argParser.addArgument("foo").nargs(1).help("Required command line string.");
But that will (for some reason) parse it as a list of one entry.  So to get the value, you need
        String fooValue= (String)namespace.getList("foo").get(0);
 
Reply all
Reply to author
Forward
0 new messages