Skip to content

Conversation

@VictorH028
Copy link

  • 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() {
Copy link
Owner

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"},
Copy link
Owner

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"},
Copy link
Owner

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

@REAndroid
Copy link
Owner

Looks neat, thank you @VictorH028

@VictorH028 VictorH028 closed this by deleting the head repository Apr 12, 2025
@REAndroid
Copy link
Owner

@VictorH028 No need to close it, I was about to merge it :(

@VictorH028
Copy link
Author

VictorH028 commented Apr 13, 2025

It was that I deleted the fork.
Sorry for the inconvenience caused. @REAndroid

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);
    }
}

@VictorH028
Copy link
Author

Then I share with you some ideas I have

@REAndroid
Copy link
Owner

I already made a review your commits with remarks. Maybe you didn't see or something wrong with your browser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants