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
Executing a single command and exitting #159
Comments
Have a look at https://docs.spring.io/spring-shell/docs/2.0.0.BUILD-SNAPSHOT/reference/htmlsingle/#_customizing_command_line_options_behavior to find out how to either:
Hope that helps |
Thanks for the prompt response. The second option sounds promising, I will pursue that. Is there a reason why the single command execution was taken out of the project in version 2.0? |
No problem. The previous approach was removed because accepting any program argument and assuming it is a command is too fragile, especially in the era of Spring Boot which makes using Another approach which may work (but I haven't entirely tested yet) is to pipe your command into the shell, ie |
Is it ok if I close this issue, or do you think there is more that needs to be done here? |
FYI, for anybody else looking to do the same thing, I found a nice little work-around. Rather than creating an public class JCommanderImportCommands {
public static enum DataType { SAMPLE, GENE, DATA }
@Parameter(names = { "-f", "--file" }, required = true, description = "Data file")
private File file;
@Parameter(names = { "-t", "--type" }, required = true, description = "Data type")
private DataType dataType;
@Parameter(names = { "-o", "--overwrite" }, description = "Flag to overwrite file if it exists")
private Boolean overwrite = false;
/* getters and setters */
}
@ShellComponent
public class ImportCommands {
@ShellMethod(key = "import", value = "Imports the a file of a specified type.")
public String jCommanderFileImport(@ShellOption(optOut = true) JCommanderImportCommands commands){
System.out.println(String.format("Importing file=%s dataType=%s overwrite=%s",
commands.getFile(), commands.getDataType(), commands.getOverwrite()));
return commands.getFile().getAbsolutePath();
}
}
public class JCommanderShellRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(args.getNonOptionArgs());
JCommanderImportCommands importCommands = new JCommanderImportCommands();
JCommander.newBuilder()
.addCommand("import", importCommands)
.acceptUnknownOptions(true)
.build()
.parse(args.getSourceArgs());
System.out.println(String.format("Importing file=%s dataType=%s overwrite=%s",
importCommands.getFile(), importCommands.getDataType(), importCommands.getOverwrite()));
}
}
@Configuration
@Profile({"jc"})
public class ShellConfig {
@Bean
public JCommanderShellRunner shellRunner(){
return new JCommanderShellRunner();
}
}
@SpringBootApplication
public class Application {
public static void main(String[] args) throws IOException {
String[] profiles = getActiveProfiles(args);
SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);
builder.bannerMode((Mode.LOG));
builder.web(false);
builder.profiles(profiles);
System.out.println(String.format("Command line arguments: %s Profiles: %s",
Arrays.asList(args), Arrays.asList(profiles)));
builder.run(args);
}
private static String[] getActiveProfiles(String[] args){
return args.length > 0 ? new String[]{"jc"} : new String[]{};
}
} Source code here. |
I had issues piping the command the command in (I think possibly because the input reader would get null on expecting the next command). The command would run fine, but the would get a bad exit. I have the following which works for me (I am using jool and lombok) as above you can easily use profiles to inject this bean or use the interactive shell. pretty much copy paste of DefaultShellApplicationRunner method except it wraps the JLineInput. To run I just use:
|
Hello @Lovett1991, i'm not really sure how to use your solution. could you elaborate a little on how to integrate this class?
|
In your config you'll need to define the bean:
|
That was the missing piece! Thanks! |
What about to exit cleanly in echo solution and do not code more? |
@ericbottard I am not sure to understand the reason why "The previous approach was removed because accepting any program argument and assuming it is a command is too fragile". |
I stumbled upon this question on Stack Overflow, explaining how to get a Spring Shell application to exit after calling it from the command line with a single command. However, testing this in 2.0.0 with Spring Boot, it does not seem to be the case any more that invoking the JAR with command arguments will execute that command and then exit. The shell just starts as normal without executing the supplied command. Is it still possible to do this? If not, would it be possible to pass the arguments from the JAR execution to Spring Shell and then trigger an exit after execution?
The text was updated successfully, but these errors were encountered: