Skip to content

Commit 4b72438

Browse files
committed
Implemented jCommander replacing GetOpt
This includes a test class to ensure it's working correctly
1 parent 5c07263 commit 4b72438

File tree

7 files changed

+435
-122
lines changed

7 files changed

+435
-122
lines changed

pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>org.fross</groupId>
55
<artifactId>cal</artifactId>
6-
<version>2.3.18</version>
6+
<version>2.4.0</version>
77
<packaging>jar</packaging>
88

99
<name>cal</name>
@@ -179,11 +179,11 @@
179179
</repositories>
180180

181181
<dependencies>
182-
<!-- https://mvnrepository.com/artifact/gnu.getopt/java-getopt -->
182+
<!-- https://mvnrepository.com/artifact/com.beust/jcommander -->
183183
<dependency>
184-
<groupId>gnu.getopt</groupId>
185-
<artifactId>java-getopt</artifactId>
186-
<version>1.0.13</version>
184+
<groupId>com.beust</groupId>
185+
<artifactId>jcommander</artifactId>
186+
<version>1.82</version>
187187
</dependency>
188188

189189
<!-- https://mvnrepository.com/artifact/org.fusesource.jansi/jansi -->

snap/snapcraft.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: fcal
2-
version: '2.3.18'
2+
version: '2.4.0'
33
summary: Command line calendar display
44
description: |
55
fCal is a command line calendar display utility. It will display a

src/main/java/org/fross/cal/Calendar.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ public static void setCalsPerRow(int cpr) {
6161
}
6262
}
6363

64+
/**
65+
* queryCalsPerRow(): Return the current number of calendars printed per row
66+
*
67+
* @return
68+
*/
69+
public static int queryCalsPerRow() {
70+
return calsPerRow;
71+
}
72+
6473
/**
6574
* firstDay(): Given the month, day, and year, return which day of the week it falls
6675
*
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/******************************************************************************
2+
* Cal - A command line calendar utility
3+
*
4+
* Copyright (c) 2019-2022 Michael Fross
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
******************************************************************************/
25+
package org.fross.cal;
26+
27+
import java.util.ArrayList;
28+
import java.util.List;
29+
30+
import org.fross.library.Debug;
31+
import org.fross.library.GitHub;
32+
import org.fross.library.Output;
33+
import org.fusesource.jansi.Ansi;
34+
35+
import com.beust.jcommander.JCommander;
36+
import com.beust.jcommander.Parameter;
37+
import com.beust.jcommander.ParameterException;
38+
39+
public class CommandLineArgs {
40+
static CommandLineArgs cli = new CommandLineArgs();
41+
static JCommander jc = new JCommander();
42+
static int monthToUse = org.fross.library.Date.getCurrentMonth();
43+
static int yearToUse = org.fross.library.Date.getCurrentYear();
44+
45+
// ---------------------------------------------------------------------------------------------
46+
// Define command line options that can be used
47+
// ---------------------------------------------------------------------------------------------
48+
49+
@Parameter(names = { "-h", "-?", "--help" }, help = true, description = "Display cal help and exit")
50+
protected boolean clHelp = false;
51+
52+
@Parameter(names = { "-z", "--no-color" }, description = "Disable colorized output")
53+
protected boolean clNoColor = false;
54+
55+
@Parameter(names = { "-v", "--version" }, description = "Show current program version and latest release on GitHub")
56+
protected boolean clVersion = false;
57+
58+
@Parameter(names = { "-D", "--debug" }, description = "Turn on Debug mode to display extra program information")
59+
protected boolean clDebug = false;
60+
61+
@Parameter(names = { "-n", "--num" }, description = "Number of calendar months to display per row")
62+
protected int clNum = Calendar.DEFAULT_CALS_PER_ROW;
63+
64+
@Parameter(description = "Month and/or Year")
65+
protected List<String> clMonthAndOrYear = new ArrayList<>();
66+
67+
// ---------------------------------------------------------------------------------------------
68+
// Process command line parameters with the following methods
69+
// ---------------------------------------------------------------------------------------------
70+
public static void ProcessCommandLine(String[] argv) {
71+
// JCommander parses the command line
72+
try {
73+
jc.setProgramName("cal");
74+
jc = JCommander.newBuilder().addObject(cli).build();
75+
jc.parse(argv);
76+
} catch (ParameterException ex) {
77+
System.out.println(ex.getMessage());
78+
jc.usage();
79+
System.exit(0);
80+
}
81+
82+
// ---------------------------------------------------------------------------------------------
83+
// Process the parsed command line options
84+
// ---------------------------------------------------------------------------------------------
85+
// Debug Switch
86+
if (cli.clDebug == true)
87+
Debug.enable();
88+
89+
// Set the stack name and restore stack from Preferences
90+
if (cli.clNum != Calendar.DEFAULT_CALS_PER_ROW) {
91+
try {
92+
if (cli.clNum <= 0) {
93+
throw new UnsupportedOperationException();
94+
}
95+
} catch (Exception Ex) {
96+
Output.fatalError("Invalid option for -n switch: '" + cli.clNum + "'", 0);
97+
}
98+
Calendar.setCalsPerRow(cli.clNum);
99+
}
100+
101+
// Version Switch
102+
if (cli.clVersion == true) {
103+
Output.printColorln(Ansi.Color.WHITE, "Cal Version: v" + Main.VERSION);
104+
Output.printColorln(Ansi.Color.CYAN, Main.COPYRIGHT);
105+
Output.printColorln(Ansi.Color.WHITE, "\nLatest Release on GitHub: " + GitHub.updateCheck("cal"));
106+
Output.printColorln(Ansi.Color.CYAN, "HomePage: https://github.com/frossm/cal");
107+
System.exit(0);
108+
}
109+
110+
// Disable Colorized Output Switch
111+
if (cli.clNoColor == true) {
112+
Output.enableColor(false);
113+
}
114+
115+
// Show Help and Exit
116+
if (cli.clHelp == true) {
117+
Help.display();
118+
System.exit(0);
119+
}
120+
121+
// Process any month/year parameters that are given and set monthToUse and yeareToUse
122+
try {
123+
switch (cli.clMonthAndOrYear.size()) {
124+
125+
// Process no dates provided
126+
case 0:
127+
Output.debugPrint("No Month or Year provided on command line. Showing current year: " + yearToUse);
128+
break;
129+
130+
// Just a date or month provided
131+
case 1:
132+
int d = Integer.parseInt(cli.clMonthAndOrYear.get(0));
133+
134+
// Number must be a year if it's greater than 12
135+
if (d > 12) {
136+
yearToUse = d;
137+
Output.debugPrint("Commandline Year provided. Showing Year: " + yearToUse);
138+
139+
// If number is <= 12, assume it's a month
140+
} else {
141+
monthToUse = d;
142+
Output.debugPrint("Commandline Month provided. Using Month: " + monthToUse + " Year:" + yearToUse);
143+
}
144+
break;
145+
146+
// Month & year provided
147+
case 2:
148+
monthToUse = Integer.parseInt(cli.clMonthAndOrYear.get(0));
149+
yearToUse = Integer.parseInt(cli.clMonthAndOrYear.get(1));
150+
Output.debugPrint("Commandline Month & Year provided. Month: " + monthToUse + " Year: " + yearToUse);
151+
break;
152+
}
153+
154+
} catch (NumberFormatException ex) {
155+
Output.fatalError("Parameters can only be numbers. Usage '-h' for options", 99);
156+
157+
} catch (Exception ex) {
158+
ex.getMessage();
159+
ex.printStackTrace();
160+
Output.fatalError("Something went very wrong. You shouldn't really see this. Eeek!", 99);
161+
}
162+
163+
}
164+
165+
/**
166+
* Return the month to use after processing the command line
167+
*
168+
* @return
169+
*/
170+
public static int queryMonthToUse() {
171+
return monthToUse;
172+
}
173+
174+
/**
175+
* Return the year to use after processing the command line
176+
*
177+
* @return
178+
*/
179+
public static int queryYearToUse() {
180+
return yearToUse;
181+
}
182+
}

0 commit comments

Comments
 (0)