spring-projects / spring-shell

Spring based shell
http://projects.spring.io/spring-shell/
Apache License 2.0
723 stars 395 forks source link

Are Pojo Parameters Possible? #310

Open danielburrell opened 3 years ago

danielburrell commented 3 years ago

Given a complex command that has say 10 or more arguments, is it possible to annotate a pojo in the way method arguments would be annotated and pass a single pojo instead?

To illustrate

 @ShellMethod(value = "Install", group = "Installation")
    public String install(
            @ShellOption(help = "....") File param1,
            @ShellOption(help = "....") File param2,
            @ShellOption(help = "....") File param3,
            @ShellOption(help = "....") File param4,
            @ShellOption(help = "....") File param5,
            @ShellOption(help = "....") File param6) {

}

to this:

   @ShellMethod(value = "Install", group = "Installation")
    public String install(@ShellPojoOrSomething Pojo pojo)    {
    }

   ....

   public class Pojo {
            @ShellOption(help = "....") File param2,
            @ShellOption(help = "....") File param3,
            @ShellOption(help = "....") File param4,
            @ShellOption(help = "....") File param5,
            @ShellOption(help = "....") File param6
           //getters and setters follow
   }
radhe-kishan commented 2 years ago

https://docs.spring.io/spring-shell/docs/current-SNAPSHOT/reference/htmlsingle/#_customizing_arguments_conversion Doesn't this fits your requirement?

@ShellComponent
class ConversionCommands {

        @ShellMethod("Shows conversion using Spring converter")
        public String conversionExample(DomainObject object) {
                return object.getClass();
        }

}

class DomainObject {
        private final String value;

        DomainObject(String value) {
                this.value = value;
        }

        public String toString() {
                return value;
        }
}

@Component
class CustomDomainConverter implements Converter<String, DomainObject> {

        @Override
        public DomainObject convert(String source) {
                return new DomainObject(source);
        }
}
lanyuanxiaoyao commented 2 years ago

docs.spring.io/spring-shell/docs/current-SNAPSHOT/reference/htmlsingle/#_customizing_arguments_conversion Doesn't this fits your requirement?

@ShellComponent
class ConversionCommands {

        @ShellMethod("Shows conversion using Spring converter")
        public String conversionExample(DomainObject object) {
                return object.getClass();
        }

}

class DomainObject {
        private final String value;

        DomainObject(String value) {
                this.value = value;
        }

        public String toString() {
                return value;
        }
}

@Component
class CustomDomainConverter implements Converter<String, DomainObject> {

        @Override
        public DomainObject convert(String source) {
                return new DomainObject(source);
        }
}

Not this. I know what he say because i meet the same question. I think that we hope to define the parameters in a pojo, and use it as normal parameters. For example, i defined a pojo

public class Pojo {
  String name;
  Integer age;

  public String toString() {
    return name + " " + age;
  }
}

and put it into shell method

@ShellMethod
public void test(Pojo pojo) { 
  System.out.println(pojo); 
}

then i could use it in commamd line like this:

spring-shell:-> test --name Tony --age 11
Tony 11
chisui commented 1 year ago

Are there any plans to support this?

Jarjanazy commented 1 year ago

@jvalkeal I would like to work on this request. This is my first contribution to Spring Shell.

pd-gov commented 1 year ago

It would be helpful if this would also work with jdk14 records.

@ShellCommandGroup("My Group")
class MyGroup {
    record CommonArgs(
        @ShellOption String a,
        @ShellOption String b) {}

    @ShellMethod
    public void hello(@ShellOptions CommonArgs common) {
        ...
    }

    @ShellMethod
    public void goodby(@ShellOptions CommonArgs common) {
        ...
    }
}
nexen505 commented 10 months ago

Hi. Any updates on this?

aminchegeni commented 8 months ago

Hi everyone

This feature very similar to @BeanParam in JAX-RS specification. You can get a lot of ideas from this annotation and its spec to implement new feature in spring framework.

I like contribute to this implementation too, if possible.