Skip to content

Commit 24029c5

Browse files
yohsukecmaglie
authored andcommitted
CLI: Add --version to CLI option (#7549)
* CLI: Add --version to CLI option I added to get the Arduino IDE version from the command line It will allow to check easily if the new Arduino is already installed. This feature makes it easier to build external systems linked to specific versions of Arduino. 1. I added `--version` action, which shows version name and exit 1. Currently, VERSION_NAME_LONG (like `1.8.5`, `1.9.0-beta`, `1.8.6 Hourly Build XXX`, etc...) is used. Because I want to know its version number and stable/beta/hourly. 2. Finish with `0`. Because it is `SUCCESSFLLY FINISHED`. 2. Updated man page. * Split "parse" and "action". Move print action to probably suitable place. This commit will fix the behavior of multiple actions about --version. * add testcase
1 parent 60021c1 commit 24029c5

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

Diff for: app/src/processing/app/Base.java

+3
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,9 @@ public Base(String[] args) throws Exception {
487487
System.exit(0);
488488
} else if (parser.isGetPrefMode()) {
489489
BaseNoGui.dumpPrefs(parser);
490+
} else if (parser.isVersionMode()) {
491+
System.out.print("Arduino: " + BaseNoGui.VERSION_NAME_LONG);
492+
System.exit(0);
490493
}
491494
}
492495

Diff for: app/test/processing/app/CommandLineTest.java

+33-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.io.File;
3535

3636
import org.apache.commons.compress.utils.IOUtils;
37+
import org.fest.assertions.Assertions;
3738
import org.junit.Before;
3839
import org.junit.Test;
3940

@@ -126,5 +127,36 @@ public void testCommandLinePreferencesSave() throws Exception {
126127

127128
prefs = new PreferencesMap(prefFile);
128129
assertEquals("preference should be saved", "xxx", prefs.get("test_pref"));
129-
}
130+
}
131+
132+
@Test
133+
public void testCommandLineVersion() throws Exception {
134+
Runtime rt = Runtime.getRuntime();
135+
Process pr = rt.exec(new String[]{
136+
arduinoPath.getAbsolutePath(),
137+
"--version",
138+
});
139+
pr.waitFor();
140+
141+
Assertions.assertThat(pr.exitValue())
142+
.as("Process will finish with exit code 0 in --version")
143+
.isEqualTo(0);
144+
Assertions.assertThat(new String(IOUtils.toByteArray(pr.getInputStream())))
145+
.matches("Arduino: \\d+\\.\\d+\\.\\d+.*");
146+
}
147+
148+
@Test
149+
public void testCommandLineMultipleAction() throws Exception {
150+
Runtime rt = Runtime.getRuntime();
151+
Process pr = rt.exec(new String[]{
152+
arduinoPath.getAbsolutePath(),
153+
"--version",
154+
"--verify",
155+
});
156+
pr.waitFor();
157+
158+
Assertions.assertThat(pr.exitValue())
159+
.as("Multiple Action will be rejected")
160+
.isEqualTo(3);
161+
}
130162
}

Diff for: arduino-core/src/processing/app/helpers/CommandlineParser.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public class CommandlineParser {
1717

1818
private enum ACTION {
19-
GUI, NOOP, VERIFY("--verify"), UPLOAD("--upload"), GET_PREF("--get-pref"), INSTALL_BOARD("--install-boards"), INSTALL_LIBRARY("--install-library");
19+
GUI, NOOP, VERIFY("--verify"), UPLOAD("--upload"), GET_PREF("--get-pref"), INSTALL_BOARD("--install-boards"), INSTALL_LIBRARY("--install-library"), VERSION("--version");
2020

2121
final String value;
2222

@@ -52,6 +52,7 @@ public CommandlineParser(String[] args) {
5252
actions.put("--get-pref", ACTION.GET_PREF);
5353
actions.put("--install-boards", ACTION.INSTALL_BOARD);
5454
actions.put("--install-library", ACTION.INSTALL_LIBRARY);
55+
actions.put("--version", ACTION.VERSION);
5556
}
5657

5758
public void parseArgumentsPhase1() {
@@ -340,6 +341,10 @@ public boolean isInstallLibrary() {
340341
return action == ACTION.INSTALL_LIBRARY;
341342
}
342343

344+
public boolean isVersionMode() {
345+
return action == ACTION.VERSION;
346+
}
347+
343348
public String getBoardToInstall() {
344349
return this.boardToInstall;
345350
}

Diff for: build/shared/manpage.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ SYNOPSIS
3333

3434
*arduino* [*--install-library* __library name__[:__version__][,__library name__[:__version__],__library name__[:__version__]]
3535

36+
*arduino* [*--version*]
37+
3638
DESCRIPTION
3739
-----------
3840
The 'arduino' integrated development environment allows editing,
@@ -83,6 +85,9 @@ ACTIONS
8385
Fetches available libraries list and install the specified one. If __version__ is omitted, the latest is installed. If a library with the same version is already installed, nothing is installed and program exits with exit code 1. If a library with a different version is already installed, it's replaced.
8486
Multiple libraries can be specified, separated by a comma.
8587

88+
*--version*::
89+
Print the version information and exit.
90+
8691
OPTIONS
8792
-------
8893
*--board* __package__:__arch__:__board__[:__parameters__]::

0 commit comments

Comments
 (0)