Refactor the smali parameters so that the debug options aren't shown unless -? is specified twice (-??)

git-svn-id: https://smali.googlecode.com/svn/trunk@643 55b6fa8a-2a1e-11de-a435-ffa8d773f76a
This commit is contained in:
JesusFreke@JesusFreke.com 2010-02-22 07:02:22 +00:00
parent f25f713426
commit 6d8f776838
2 changed files with 98 additions and 51 deletions

View File

@ -44,9 +44,13 @@ public class main {
public static final String VERSION; public static final String VERSION;
private final static Options basicOptions;
private final static Options debugOptions;
private final static Options options; private final static Options options;
static { static {
basicOptions = new Options();
debugOptions = new Options();
options = new Options(); options = new Options();
buildOptions(); buildOptions();
@ -91,14 +95,43 @@ public class main {
String[] remainingArgs = commandLine.getArgs(); String[] remainingArgs = commandLine.getArgs();
if (commandLine.hasOption("v")) { Option[] options = commandLine.getOptions();
version();
return;
}
if (commandLine.hasOption("?")) { for (int i=0; i<options.length; i++) {
usage(); Option option = options[i];
return; String opt = option.getOpt();
switch (opt.charAt(0)) {
case 'v':
version();
return;
case '?':
while (++i < options.length) {
if (options[i].getOpt().charAt(0) == '?') {
usage(true);
return;
}
}
usage(false);
return;
case 'o':
outputDexFile = commandLine.getOptionValue("o");
break;
case 'D':
dumpFileName = commandLine.getOptionValue("D", outputDexFile + ".dump");
break;
case 'S':
sort = true;
break;
case 'C':
fixStringConst = false;
break;
case 'G':
fixGoto = false;
break;
default:
assert false;
}
} }
if (remainingArgs.length == 0) { if (remainingArgs.length == 0) {
@ -106,29 +139,7 @@ public class main {
return; return;
} }
if (commandLine.hasOption("o")) {
outputDexFile = commandLine.getOptionValue("o");
}
if (commandLine.hasOption("d")) {
dumpFileName = commandLine.getOptionValue("d", outputDexFile + ".dump");
}
if (commandLine.hasOption("s")) {
sort = true;
}
if (commandLine.hasOption("c")) {
fixStringConst = false;
}
if (commandLine.hasOption("g")) {
fixGoto = false;
}
try { try {
LinkedHashSet<File> filesToProcess = new LinkedHashSet<File>(); LinkedHashSet<File> filesToProcess = new LinkedHashSet<File>();
for (String arg: remainingArgs) { for (String arg: remainingArgs) {
@ -263,10 +274,25 @@ public class main {
/** /**
* Prints the usage message. * Prints the usage message.
*/ */
private static void usage() { private static void usage(boolean printDebugOptions) {
HelpFormatter formatter = new HelpFormatter(); smaliHelpFormatter formatter = new smaliHelpFormatter();
formatter.setWidth(100);
formatter.printHelp("java -jar smali.jar [options] [--] [<smali-file>|folder]*", formatter.printHelp("java -jar smali.jar [options] [--] [<smali-file>|folder]*",
"assembles a set of smali files into a dex file, and optionally generats an annotated dump of the output file", options, ""); "assembles a set of smali files into a dex file", basicOptions, "");
if (printDebugOptions) {
System.out.println();
System.out.println("Debug Options:");
StringBuffer sb = new StringBuffer();
formatter.renderOptions(sb, debugOptions);
System.out.println(sb.toString());
}
}
private static void usage() {
usage(false);
} }
/** /**
@ -279,47 +305,54 @@ public class main {
System.exit(0); System.exit(0);
} }
private static void buildOptions() { private static void buildOptions() {
Option versionOption = OptionBuilder.withLongOpt("version") Option versionOption = OptionBuilder.withLongOpt("version")
.withDescription("prints the version then exits") .withDescription("prints the version then exits")
.create("v"); .create("v");
Option helpOption = OptionBuilder.withLongOpt("help") Option helpOption = OptionBuilder.withLongOpt("help")
.withDescription("prints the help message then exits") .withDescription("prints the help message then exits. Specify twice for debug options")
.create("?"); .create("?");
Option dumpOption = OptionBuilder.withLongOpt("dump-to")
.withDescription("additionally writes a dump of written dex file to FILE (<dexfile>.dump by default)")
.hasOptionalArg()
.withArgName("FILE")
.create("d");
Option outputOption = OptionBuilder.withLongOpt("output") Option outputOption = OptionBuilder.withLongOpt("output")
.withDescription("the name of the dex file that will be written. The default is out.dex") .withDescription("the name of the dex file that will be written. The default is out.dex")
.hasArg() .hasArg()
.withArgName("FILE") .withArgName("FILE")
.create("o"); .create("o");
Option dumpOption = OptionBuilder.withLongOpt("dump-to")
.withDescription("additionally writes a dump of written dex file to FILE (<dexfile>.dump by default)")
.hasOptionalArg()
.withArgName("FILE")
.create("D");
Option sortOption = OptionBuilder.withLongOpt("sort") Option sortOption = OptionBuilder.withLongOpt("sort")
.withDescription("sort the items in the dex file into a canonical order before writing") .withDescription("sort the items in the dex file into a canonical order before writing")
.create("s"); .create("S");
Option noFixStringConstOption = OptionBuilder.withLongOpt("no-fix-string-const") Option noFixStringConstOption = OptionBuilder.withLongOpt("no-fix-string-const")
.withDescription("Don't replace string-const instructions with string-const/jumbo where appropriate") .withDescription("Don't replace string-const instructions with string-const/jumbo where appropriate")
.create("c"); .create("C");
Option noFixGotoOption = OptionBuilder.withLongOpt("no-fix-goto") Option noFixGotoOption = OptionBuilder.withLongOpt("no-fix-goto")
.withDescription("Don't replace goto type instructions with a larger version where appropriate") .withDescription("Don't replace goto type instructions with a larger version where appropriate")
.create("g"); .create("G");
options.addOption(versionOption); basicOptions.addOption(versionOption);
options.addOption(helpOption); basicOptions.addOption(helpOption);
options.addOption(dumpOption); basicOptions.addOption(outputOption);
options.addOption(outputOption);
options.addOption(sortOption); debugOptions.addOption(dumpOption);
options.addOption(noFixStringConstOption); debugOptions.addOption(sortOption);
options.addOption(noFixGotoOption); debugOptions.addOption(noFixStringConstOption);
debugOptions.addOption(noFixGotoOption);
for (Object option: basicOptions.getOptions()) {
options.addOption((Option)option);
}
for (Object option: debugOptions.getOptions()) {
options.addOption((Option)option);
}
} }
} }

View File

@ -0,0 +1,14 @@
package org.jf.smali;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
public class smaliHelpFormatter extends HelpFormatter {
public void smaliHelpFormatter() {
}
public void renderOptions(StringBuffer sb, Options options) {
super.renderOptions(sb, getWidth(), options, getLeftPadding(), this.getDescPadding());
}
}