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

Adds path argument #404

Merged
merged 2 commits into from
Oct 9, 2024
Merged

Adds path argument #404

merged 2 commits into from
Oct 9, 2024

Conversation

moonlander101
Copy link
Contributor

@moonlander101 moonlander101 commented Oct 9, 2024

fix: #402

Adds the path argument to the CLI so that biscuit <path> would start the editor and open said directory.

@tomlin7
Copy link
Owner

tomlin7 commented Oct 9, 2024

@moonlander101 Simply adding the click argument won't work in this case, this will shadow the subcommands like biscuit clone biscuit diff biscuit ext (breaks rest of the CLI except opening folder)

There are several approaches to have both running without conflict.
First of all let's check the current output when we run biscuit . (or any path)

$ biscuit .    
Usage: biscuit [OPTIONS] COMMAND [ARGS]...

Error: No such command '.'.
  • Define a custom Group class to use, override it's main method to handle fail exceptions thrown
  • Use this to parse the arguments as path and open it in an app instance

@tomlin7
Copy link
Owner

tomlin7 commented Oct 9, 2024

@moonlander101 here are the final changes made

  • Added BiscuitCLI custom Group class and used that for the cli group cls=BiscuitCLI
  • Overrided main method to catch exception
  • If exception is caught, use the path to open new Biscuit instance

Accessing the path argument can be done in many ways
In following code resolve_command method of click.Group is overriden

  • stores the make_str(args[0]) as a class member, now accessible anywhere in class
  • also suppresses the failure messages shown by click by default when subcommand is not found
from click.parser import split_opt
from click.utils import make_str
...

class BiscuitCLI(click.Group):
    path = ""

    def main(self, *args, **kwargs):
        try:
            return super(BiscuitCLI, self).main(*args, **kwargs)
        except Exception:
            path = self.path

            if path:
                path = Path(self.path).resolve()
                path.mkdir(exist_ok=True)

            appdir = Path(__file__).resolve().parent
            app = get_app_instance(appdir, open_path=str(path))
            app.run()
   
    def resolve_command(self, ctx, args):
        cmd_name = make_str(args[0])
        original_cmd_name = cmd_name

        cmd = self.get_command(ctx, cmd_name)
        if cmd is None and ctx.token_normalize_func is not None:
            cmd_name = ctx.token_normalize_func(cmd_name)
            cmd = self.get_command(ctx, cmd_name)

        if cmd is None and not ctx.resilient_parsing:
            if split_opt(cmd_name)[0]:
                self.parse_args(ctx, ctx.args)

            # ctx.fail(_("No such command {name!r}.").format(name=original_cmd_name))
            self.path = original_cmd_name

        return cmd_name if cmd else None, cmd, args[1:]

@click.group(cls=BiscuitCLI, invoke_without_command=True)
def cli(): ...

@tomlin7 tomlin7 merged commit cdecb1a into tomlin7:main Oct 9, 2024
3 of 5 checks passed
@tomlin7 tomlin7 added the hacktoberfest-accepted Hacktoberfest accepted pr label Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest-accepted Hacktoberfest accepted pr
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CLI: Implicitly opening passed directory
2 participants