Skip to content

Commit

Permalink
improve Cmd and ArgumentParser
Browse files Browse the repository at this point in the history
  • Loading branch information
andrescv committed Nov 18, 2018
1 parent 270afdf commit 2cb1b63
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 49 deletions.
2 changes: 1 addition & 1 deletion build.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# current V-Sim version
version = 1.0.2
version = 1.0.3
# source directory
src.dir = vsim
# lib directory
Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@

# prints V-Sim version
vsim_version() {
echo 'v1.0.2'
echo 'v1.0.3'
}

# prints V-Sim jar file
Expand Down
2 changes: 1 addition & 1 deletion vsim/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public final class Settings {

/** current version */
public static final String VERSION = "v1.0.2";
public static final String VERSION = "v1.0.3";

/** installation path */
public static String ROOT = null;
Expand Down
37 changes: 17 additions & 20 deletions vsim/utils/ArgumentParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public final class ArgumentParser {
private HashMap<String, Integer> flags;
/** stores the values that were set */
private HashMap<Integer, String> values;
/** stores aliases of values that were set */
private HashMap<String, String> aliases;
/** stores the valid options for this parser */
private TreeMap<String, String> options;
/** stores the valid options that requires a value */
Expand All @@ -56,37 +58,31 @@ public ArgumentParser(String usage) {
this.usage = usage;
this.flags = new HashMap<String, Integer>();
this.values = new HashMap<Integer, String>();
this.aliases = new HashMap<String, String>();
this.options = new TreeMap<String, String>();
this.optsval = new HashSet<String>();
this.targets = new ArrayList<String>();
this.errors = new ArrayList<String>();
}

/**
* Constructor that initializes a newly ArgumentParser object with
* a default usage message {@code "[options]"}.
*/
public ArgumentParser() {
this("[options]");
}

/**
* This method adds a new option to the valid option list.
*
* @param option the option that starts with -
* @param help help information attached to this option
* @param requiresValue if this option requires a value
* @param alias alias for value if this option requires a value, otherwise null
*/
public void add(String option, String help, boolean requiresValue) {
public void add(String option, String help, String alias) {
// only include options that starts with '-'
if (option != null && option.startsWith("-")) {
this.options.put(option, help);
// this option requires a value
if (requiresValue) {
if (alias != null) {
this.optsval.add(option);
this.maxLength = Math.max(this.maxLength, option.length() * 2 + 2);
this.aliases.put(option, alias);
this.maxLength = Math.max(this.maxLength, option.length() + alias.length() + 2);
} else
this.maxLength = Math.max(this.maxLength, option.length() + 2);
this.maxLength = Math.max(this.maxLength, option.length() + 1);
}
}

Expand All @@ -98,7 +94,7 @@ public void add(String option, String help, boolean requiresValue) {
* @param help help information attached to this option
*/
public void add(String option, String help) {
this.add(option, help, false);
this.add(option, help, null);
}

/**
Expand All @@ -124,7 +120,7 @@ public void parse(String[] args) {
lastFlag = args[i];
// valid flag ?
if (!this.options.containsKey(args[i]))
this.errors.add("unknown argument: " + args[i]);
this.errors.add("unknown option: " + args[i]);
this.flags.put(args[i], i);
} else
// store every value
Expand Down Expand Up @@ -174,12 +170,13 @@ public void print() {
out += "available options:" + newline;
for (String option: this.options.keySet()) {
out += " " + option;
int length = option.length();
if (this.optsval.contains(option)) {
out += " " + option.substring(1).toUpperCase();
length += option.length();
int length = option.length() + 1;
if (this.aliases.get(option) != null) {
String alias = this.aliases.get(option);
out += " " + alias;
length += alias.length() + 1;
}
for (int i = 0; i < (this.maxLength - length); i++)
for (int i = 0; i < (this.maxLength - length + 1); i++)
out += " ";
out += this.options.get(option) + newline;
}
Expand Down
74 changes: 48 additions & 26 deletions vsim/utils/Cmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,29 @@ public static ArrayList<String> parse(String[] args) {
ArgumentParser parser = new ArgumentParser("vsim [options] <files>");
// simulator available options
parser.add("-help", "show this help message and exit");
parser.add("-all", "assemble all files in directory");
parser.add("-bare", "bare machine (no pseudo-ops)");
parser.add("-quiet", "do not print warnings");
parser.add("-nocolor", "do not colorize output");
parser.add("-usage", "print usage of an instruction and exit", true);
parser.add("-notitle", "do not print title and copyright notice");
parser.add("-dump", "dump machine code to a file", true);
parser.add("-start", "start program at global label (default: main)", true);
parser.add("-usage", "print usage of an instruction and exit", "<mnemonic>");
parser.add("-notitle", "do not print V-Sim title");
parser.add("-dump", "dump machine code to a file", "<file>");
parser.add("-start", "start program at global label (default: main)", "<label>");
parser.add("-debug", "start the debugger");
parser.add("-version", "show the simulator version and exit");
parser.add("-license", "show license and copyright notice and exit");
parser.add("-iset", "print available RISC-V instructions and exit");
// parse args
parser.parse(args);
// display usage if errors
if (parser.hasErrors()) {
Cmd.title();
for (String error: parser.getErrors())
Message.warning(error);
IO.stdout.println();
parser.print();
System.exit(1);
}
// override default Settings
if (parser.hasFlag("-bare"))
Settings.BARE = true;
Expand All @@ -69,33 +80,30 @@ public static ArrayList<String> parse(String[] args) {
Settings.START = parser.value("-start");
if (parser.hasFlag("-debug"))
Settings.DEBUG = true;
// display usage if errors
if (parser.hasErrors()) {
Cmd.title();
for (String error: parser.getErrors())
Message.warning(error);
IO.stdout.println();
parser.print();
System.exit(1);
}
// first -help
// check -help flag
if (parser.hasFlag("-help")) {
Cmd.title();
parser.print();
System.exit(0);
}
// second -version
// check -license flag
if (parser.hasFlag("-license")) {
Cmd.titleAndLicense();
System.exit(0);
}
// check -version flag
if (parser.hasFlag("-version")) {
IO.stdout.println(Settings.VERSION);
Cmd.title();
IO.stdout.println("Version: " + Settings.VERSION);
System.exit(0);
}
// third -iset
// check -iset flag
if (parser.hasFlag("-iset")) {
Cmd.title();
Globals.iset.print();
System.exit(0);
}
// fourth -usage
// check -usage flag
if (parser.hasFlag("-usage")) {
Cmd.title();
Globals.iset.print(parser.value("-usage"));
Expand All @@ -111,9 +119,7 @@ public static ArrayList<String> parse(String[] args) {
}
}
// check project mode
if (files.size() == 1 && files.get(0).equals(".")) {
// remove this
files.remove(0);
if (parser.hasFlag("-all")) {
try {
// recursively find all files in user cwd
Files.find(
Expand All @@ -134,7 +140,7 @@ public static ArrayList<String> parse(String[] args) {
);
} catch (Exception e) {
if (!Settings.QUIET)
Message.warning("An error occurred while recursively searching the files in the directory (aborting...)");
Message.warning("An error occurred while recursively searching the files in directory (aborting...)");
}
}
files.trimToSize();
Expand All @@ -154,16 +160,32 @@ public static void title() {
IO.stdout.println(Colorize.yellow("| |/ /") + "___" + Colorize.blue("/\\ \\/ / ' \\"));
IO.stdout.println(Colorize.yellow("|___/") + Colorize.blue(" /___/_/_/_/_/") + newline);
IO.stdout.println(Colorize.cyan("RISC-V Assembler & Runtime Simulator" + newline));
IO.stdout.println("GPL-3.0 License");
IO.stdout.println("Copyright (c) 2018 Andres Castellanos");
IO.stdout.println("All Rights Reserved");
IO.stdout.println("See the file LICENSE for a full copyright notice" + newline);
}
if (Settings.TRAP != null) {
IO.stdout.println("loaded: " + Colorize.green("traphandler.s") + newline);
}
}

/**
* This method prints the title and license of the V-Sim simulator.
*/
public static void titleAndLicense() {
// print the title and license note if the -notitle flag is not set
String newline = System.getProperty("line.separator");
if (Settings.TITLE) {
// cool title :]
IO.stdout.println(Colorize.yellow(" _ __") + Colorize.blue(" _____"));
IO.stdout.println(Colorize.yellow("| | / /") + "___" + Colorize.blue("/ __(_)_ _"));
IO.stdout.println(Colorize.yellow("| |/ /") + "___" + Colorize.blue("/\\ \\/ / ' \\"));
IO.stdout.println(Colorize.yellow("|___/") + Colorize.blue(" /___/_/_/_/_/") + newline);
IO.stdout.println(Colorize.cyan("RISC-V Assembler & Runtime Simulator" + newline));
IO.stdout.println("GPL-3.0 License");
IO.stdout.println("Copyright (c) 2018 Andres Castellanos");
IO.stdout.println("All Rights Reserved");
IO.stdout.println("See " + Colorize.green("https://git.io/fpcYS") + " for a full copyright notice");
}
}

/**
* This method prints the prompt of the V-Sim simulator.
*/
Expand Down

0 comments on commit 2cb1b63

Please sign in to comment.