-
Notifications
You must be signed in to change notification settings - Fork 348
Code Obtimisation #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code Obtimisation #177
Conversation
VictorH028
commented
Apr 9, 2025
- Smaller Methods
- Constant for exit code
- Handling Sentralized Errors
- Separate procedure
- More descriptive names
Radical changes throughout the structure
Allows manual execution from the GitHub interface
| ResourceStrings.INSTANCE, Main.class); | ||
| builder.setFooters("", "help_main_footer", "<command> -h", ""); | ||
| System.err.println(builder.build()); | ||
| private void displayHelp() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method called via reflection, thus private modifier causes accessibility error on some java versions and dalvik vm.
Same for all annotated methods
| @OtherOption( | ||
| names = {"-v", "-version"}, alternates = {"--version"}, | ||
| description = "Displays version" | ||
| names = {"-v", "--version"}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need all three switches, as discussed here #146
| @OtherOption( | ||
| names = {"-h", "-help"}, alternates = {"--help"}, | ||
| description = "Displays this help and exit" | ||
| names = {"-h", "--help"}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to gracefully depreciate first, for now please keep all switches
|
Looks neat, thank you @VictorH028 |
|
@VictorH028 No need to close it, I was about to merge it :( |
|
It was that I deleted the fork. Here I leave you the code of how I had structured everything public class Main {
private static final int EXIT_SUCCESS = 0;
private static final int EXIT_ERROR = 1;
private static final int EXIT_HELP = 2;
private Options currentOptions;
private int exitCode = EXIT_HELP;
private boolean emptyOption;
public static void main(String[] args) {
System.exit(execute(args));
}
public static int execute(String[] args) {
return new Main().run(args);
}
// Comandos principales
@OtherOption(
names = {"-h", "--help"},
description = "Display this help and exit"
)
private void displayHelp() {
exitCode = EXIT_HELP;
System.err.println(buildHelpText());
}
@OtherOption(
names = {"-v", "--version"},
description = "Display version information"
)
private void displayVersion() {
exitCode = EXIT_HELP;
System.out.println(buildVersionText());
}
// Manejador de selección de opciones
@OnOptionSelected
private void onOptionSelected(Object option, boolean emptyArgs) {
this.currentOptions = (Options) option;
this.emptyOption = emptyArgs;
}
private int run(String[] args) {
resetState();
try {
CommandParser parser = new CommandParser(Main.class);
parser.parse(this, args);
if (currentOptions == null) {
return exitCode;
}
if (emptyOption) {
throw new CommandException("empty_command_option_exception");
}
processSelectedOption();
} catch (CommandException e) {
handleCommandException(e);
} catch (EncodeException | XmlEncodeException e) {
handleEncodeException(e);
} catch (Exception e) {
handleUnexpectedException(e);
}
return exitCode;
}
// Métodos auxiliares
private void resetState() {
currentOptions = null;
exitCode = EXIT_HELP;
emptyOption = false;
}
private void processSelectedOption() throws Exception {
currentOptions.validate();
if (currentOptions.help) {
System.err.println(currentOptions.getHelp());
return;
}
exitCode = EXIT_ERROR;
currentOptions.runCommand();
exitCode = EXIT_SUCCESS;
}
private String buildHelpText() {
CommandHelpBuilder builder = new CommandHelpBuilder(
ResourceStrings.INSTANCE, Main.class);
builder.setFooters("", "help_main_footer", "<command> -h", "");
return builder.build();
}
private String buildVersionText() {
return String.format("%s version %s, %s version %s",
APKEditor.getName(), APKEditor.getVersion(),
ARSCLib.getName(), ARSCLib.getVersion());
}
private void handleCommandException(CommandException e) {
System.err.flush();
System.err.println(e.getMessage(ResourceStrings.INSTANCE));
}
private void handleEncodeException(Exception e) {
System.err.flush();
System.err.println("\nERROR:\n" + e.getMessage());
}
private void handleUnexpectedException(Exception e) {
System.err.flush();
System.err.println("\nUNEXPECTED ERROR:");
e.printStackTrace(System.err);
}
} |
|
Then I share with you some ideas I have |
|
I already made a review your commits with remarks. Maybe you didn't see or something wrong with your browser |