You can check this project out - it lists a specific adaptation of
JCommander to Guice. The read me has most of the instructions here:
https://github.com/israfil/jcommander-inject/tree/master/guice
A quit summary is:
You create your Parameters object, say:
class Arg {
@Parameter(names = "-long")
public Long l;
}
And then you can call
Module m =
JCommanderModuleBuilder.bindParameters(Arg.class).withArguments(args).build();
then create your injector using that module. You can then inject your
classes either by directly depending on Arg, or by @Named typed
arguments, like
class MyClass {
@Inject MyClass(@Named("long") Long long) {
}
}
The module builder will create a module which binds all the arguments
into named bindings, which you can inject in your classes
JCommander also uses Guice's custom injection annotation approach, but I
find that a little cumbersome, so I whipped up this. Either way.
cheers,
Christian.
On 15 Nov 2013, at 21:17, Maatary Okouya wrote:
> Many thanks for your answer, appreciated.
>
> Although i might have forgot to mention that i'm new to guice and what
> you
> wrote does not make much sense in my body of knowledge.
>
> I personally come up with a solution in the mean time that i do not
> like so
> much but still work. I use the assisted inject framework.
>
> I saw that this is what is recommended because with provider you can
> only
> set information that you know in advance, not things that you get at
> runtime. In the faq of google guice they seem to recommand the use of
> assisted inject as well. But i find it so cumbersome. The only
> advantage is
> that the factory is generated for you. It feels like an over kills
>
> With respect to your exemple, please do you mind assisting me a bit
> further
> by connecting the dots please: I don't see any provider created, the
> corresponding module that will contain it, how you will initialize the
> module and call for your instance and etc....
>
>
>
>
>
> On Saturday, November 16, 2013 4:43:06 AM UTC+1, Cédric Beust ♔
> wrote:
>>
>> I suggest using a provider and the awesome (obviously) JCommander
>> library<
http://jcommander.org>
>> .
>>
>> class Main {
>> private Arg arg = new Arg();
>>
>> public static void main(String[] args) throws Exception {
>> class Arg {
>> @Parameter(names = "-long")
>> public long l;
>> }
>>
>> new JCommander(arg).parse("-lon", "32");
>>
>> }
>>
>> @Provider
>> public Arg getArg() {
>> return arg;
>> }
>> }
>>
>> then simply
>>
>> @Injectprivate Arg arg;
>>
>> --
>> Cédric
>>
>>
>>
>> --
>> Cédric
>>
>>
>>
>> On Fri, Nov 15, 2013 at 6:21 PM, Maatary Okouya
>> <
maatar...@gmail.com<javascript:>
>>> email to
google-guice...@googlegroups.com <javascript:>.
>>> To post to this group, send email to
>>>
google...@googlegroups.com<javascript:>
>>> .