-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update jcommander (Bug fix: see cbeust/jcommander#380); adding simple…
… parsing
- Loading branch information
Showing
21 changed files
with
128 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
Manifest-Version: 1.0 | ||
Main-Class: Cipher | ||
Class-Path: ../lib/jcommander-1.72.jar | ||
Main-Class: Main | ||
Class-Path: ../lib/jcommander-1.74.jar |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import cryptography.*; | ||
import parser.*; | ||
|
||
import com.beust.jcommander.JCommander; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] argv) throws Exception { | ||
Settings settings = new Settings(); | ||
for (String arg : argv){ | ||
System.out.println(arg); | ||
} | ||
// All invalid/edge cases are caught during parsing | ||
new JCommander(settings, argv); | ||
new Main().run(settings); | ||
} | ||
|
||
public void run(Settings settings) throws Exception { | ||
System.out.println(settings.algo); | ||
System.out.println(settings.shift); | ||
switch (settings.algo) { | ||
case RSA: | ||
// TODO | ||
break; | ||
case SHIFT: | ||
Shift cipher = new Shift(); | ||
cipher.encrypt(); | ||
break; | ||
default: | ||
// Should be handled by parser, but throw exception here anyways | ||
throw new IllegalArgumentException("Invalid algorithm: " + settings.algo); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package algorithm; | ||
package cryptography; | ||
|
||
public class RSA { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package cryptography; | ||
|
||
public class Shift { | ||
|
||
public Shift() { | ||
} | ||
|
||
public void encrypt() { | ||
System.out.println("Hello, world!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package parser; | ||
|
||
import com.beust.jcommander.ParameterException; | ||
import com.beust.jcommander.IStringConverter; | ||
|
||
public class AlgoConverter implements IStringConverter<AlgoEnum> { | ||
|
||
@Override | ||
public AlgoEnum convert(String value) { | ||
AlgoEnum convertedValue = AlgoEnum.fromString(value); | ||
|
||
if (convertedValue == null) { | ||
throw new ParameterException("Value " + value + " cannot be converted to AlgoEnum. " + | ||
"Available values are: rsa, shift."); | ||
} | ||
return convertedValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package parser; | ||
|
||
public enum AlgoEnum { | ||
|
||
SHIFT, | ||
RSA; | ||
|
||
// converter | ||
public static AlgoEnum fromString(String code) { | ||
for (AlgoEnum algo : AlgoEnum.values()) { | ||
if (algo.toString().equalsIgnoreCase(code)) { | ||
return algo; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package parser; | ||
|
||
import com.beust.jcommander.Parameter; | ||
|
||
public class Settings { | ||
|
||
@Parameter(description="The algorithm to use to encrypt/decrypt your message", required=true, converter=AlgoConverter.class) | ||
public AlgoEnum algo; | ||
@Parameter(names={"--shift", "-s"}, description="The shift to apply to each character in your message", validateWith=ShiftValidator.class) | ||
public int shift; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package parser; | ||
|
||
import com.beust.jcommander.ParameterException; | ||
import com.beust.jcommander.IParameterValidator; | ||
|
||
public class ShiftValidator implements IParameterValidator { | ||
|
||
@Override | ||
public void validate(String name, String value) throws ParameterException { | ||
int shift = Integer.parseInt(value); | ||
|
||
if (shift < 1 || shift > 26) { | ||
throw new ParameterException("Parameter " + name + " should be between 1 and 26"); | ||
} | ||
} | ||
} |