-
-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Command line arguments #74
Comments
Hi. Sorry for the delay, and at the moment also github is down :( Yes, it is possible to use the command line - or a programmatic way - to indicate from where to load the properties. See Configuring, also this question relates to #35 and #36, you can have a look to discussions and examples there. Example: // notice ${mypath} here
@Sources("file:${mypath}");
interface MyConfig extends Config { ... }
public static void main(String[] args) {
String cmdLinePath = "/foo/bar/baz.properties";
if (args.length() > 0)
cmdLinePath = arg[0];
ConfigFactory.setProperty("mypath", cmdLinePath);
MyConfig cfg = ConfigFactory.create(MyConfig.class);
} Also, instead of using command line argument, you can use system properties, which are expanded by default: // notice ${mypath} here
@Sources("file:${mypath}");
interface MyConfig extends Config { ... }
MyConfig cfg = ConfigFactory.create(MyConfig.class); should work from system properties by default, if you start your app with java -Dmypath="/foo/bar/baz.properties" com.acme.MyApp Both the above will also support hot reload, of the Another option is to use Imported Properties Properties props = new Properties();
props.load(new FileInputStream(args[0]);
MyConfig cfg = ConfigFactory.create(MyConfig.class, props); or use the method interface MyMutableConfig extends Mutable { ... }
MyMutableConfig cfg = ConfigFactory.create(MyMutableConfig.class);
cfg.load(new FileInputStream(args[0])); But without using the Let me know if this answer satisfies your use cases. |
Thank you for your fast response! Sorry, i did not make myself clear enough. I don't want to tell via command line which source I want to use. I want to use the arguments (passed via commandline) themselfs as a source. E.g. I have following interface. public interface ImportConfig extends Config {
@DefaultValue("apple")
String foo();
@DefaultValue("pear")
String bar();
@DefaultValue("orange")
String baz();
} It would be great to make it possible to overwrite configuration from property file with command line arguments. E.g we load from following property file:
and we start our software with following arguments Then the configuration object generated by owner should return following values:
This functionality would be great, because i don't have to deal with command line arguments (with args4j) additionally all the time. It's often the case that I want to make the user able to overwrite configuration with arguments passed to command line. What do you think about such a feature? |
This is also possible. The OWNER library doesn't parse command line arguments; so first, you need to use something like commons-cli and get your properties overridings inside a properties object: Properties propsOverriding = parseCommandLinePropsOverridings();
ImportConfig cfg = ConfigFactory.create(ImportConfig.class, propsOverriding); To parse the command line there are several options. Or, another option is to use System properties (on the command line) to override: // to override props define system properties at the command line
// $ java -Dbaz=lemon com.acme.MyApp
//
ImportConfig cfg = ConfigFactory.create(ImportConfig.class, System.getProperties()); In this way you can redefine properties at the command line. In the same way, you can also use environment variables, with |
Using commons-cli or something is not a perfect solution, because I have to define configuration options two times that are identical. Command line arguments as first class citizen in owner would be great, especially if an enhanced validation mechanism is available to output configuration errors (#57). As far as this is not possbile, using system properties seems to be a reasonable compromise. |
In Java, when executing a command line App, the method called is something like: public static void main(String[] args) {
....
} So, the minimum you can do, is to pass But I find this out of scope for the core library. So I am planning to create a new module (maven module) to collect all utilities that can simplify common tasks like this. I'll keep this request in mind for that. In this way you'll need to add an additional jar file to the classpath to have those utilities available. A list of adapters is already in planning to allow reading configuration from web.xml, applet parameters, jndi, etc. I just updated issue #14 which is related to this. Feel free to implement your solution and propose it as integration for OWNER. |
I second the notion that command line options should be part of this lib. |
Just as an updated: I implemented this by using apache-commons-cli library together with owner. It works quite well In your
And here is the
|
Thanks, but I think a library should NOT interfere with user interface (as command line), unless the user wants to. Also, 3rd part libraries dependencies are not welcome without a very good reason. |
In fact, there is no OWNER real usage in your code: this is not what owner does or it is supposed to do. |
Is it possible to define command line arguments as source? If not, this would be a nice feature. What do you think?
The text was updated successfully, but these errors were encountered: