Skip to content
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

Argparser not implemented yet. #26

Open
1 task
Almas-Ali opened this issue Apr 1, 2024 · 1 comment
Open
1 task

Argparser not implemented yet. #26

Almas-Ali opened this issue Apr 1, 2024 · 1 comment
Labels
enhancement New feature or request stdlib Standard Library
Milestone

Comments

@Almas-Ali
Copy link
Member

Currently Argparser module not implemented yet to work. I haven't planed anything about this module yet. But this requires more improvement in the language syntax before get to work. Lot of those functionalities are depended on language syntax.

  • - Language syntax ready.

After language syntax ready to work on, then we have to implement those methods to write our first CLI based program in Radon language.

If anyone planed to work in this part, your free to do so. You can propose any standards as it is not prepared yet.

@Almas-Ali Almas-Ali added enhancement New feature or request stdlib Standard Library labels Apr 1, 2024
@Almas-Ali
Copy link
Member Author

Almas-Ali commented Apr 27, 2024

Python implementation. Basic example like builtin argparse module. Will be more advance.

import sys

class ArgumentParser:
    def __init__(self, description):
        self.options = {}
        self.description = description

    def add_option(self, option, description, is_flag=False):
        self.options[option] = {"description": description, "is_flag": is_flag}

    def parse_arguments(self):
        arguments = sys.argv
        parsed_args = {}
        current_option = None
        index = 1  # Start from index 1 to skip the script name
        
        while index < len(arguments):
            argument = arguments[index]
            if argument.startswith("--"):
                current_option = argument[2:]
                if current_option in self.options:
                    if self.options[current_option]["is_flag"]:
                        parsed_args[current_option] = True
                    else:
                        index += 1
                        if index < len(arguments):
                            parsed_args[current_option] = arguments[index]
                        else:
                            print("Value missing for option:", argument)
                            return {}
                else:
                    print("Unrecognized option:", argument)
                    return {}
            else:
                print("Unrecognized argument:", argument)
                return {}
            index += 1
        
        return parsed_args

    def print_usage(self):
        print(self.description)
        for option, info in self.options.items():
            print(f"\t--{option}\t{info['description']}")

# Example usage:
parser = ArgumentParser("This is a sample CLI tool")
parser.add_option("help", "Display usage information", is_flag=True)
parser.add_option("verbose", "Enable verbose mode", is_flag=True)
parser.add_option("output", "Specify output file")

parsed_arguments = parser.parse_arguments()

if parsed_arguments.get("help"):
    parser.print_usage()
else:
    if parsed_arguments.get("verbose"):
        print("Verbose mode enabled")
    if "output" in parsed_arguments:
        print("Output file specified:", parsed_arguments["output"])

@Almas-Ali Almas-Ali added this to the v0.1 release milestone May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request stdlib Standard Library
Projects
None yet
Development

No branches or pull requests

1 participant