sisl icon indicating copy to clipboard operation
sisl copied to clipboard

WIP: Converting `sisl_toolbox` to typer.

Open pfebrer opened this issue 10 months ago • 17 comments

Related to #606.

This PR tries to convert the sisl_toolbox CLI to typer. More importantly, it tries to create a framework for sisl CLIs to be created from type hints. Following Nick's suggestion, the work is split in two steps:

  • [x] Convert the atom CLI.
  • [x] Convert the poisson CLI and merge it with the atom one.

How it looks

This is one of the strongest points of typer (thanks to rich). I think it's indisputable that it makes for a clearer and nicer looking CLI:

Before

Screenshot from 2023-08-09 16-22-10

Screenshot from 2023-08-09 16-29-24

After

Screenshot from 2023-08-09 16-22-36

Screenshot from 2023-08-09 16-29-35

How it works

While working out the details, I found some advantages and disadvantages:

Advantages

  • I think the code looks cleaner by declaring everything as a normal function with type hints (apart from the wrapper).
  • The function can be used from inside python very easily, not just from a CLI. This means that, for example, probably the same function will also be easy to use from an API.
  • With the wrapper, parsing of arguments will be centralized so the CLIs in sisl will all behave predictably and we can introduce complex parsing for custom types (e.g. Geometry, AtomsArgument) that all CLIs would use.

Disadvantages

  • I didn't particularly like the inner workings of click as they are a bit restrictive in some cases. For example, it doesn't allow an undefined number of values for options (it does for arguments) out of the box. E.g. stoolbox --plot wavefunction log is not allowed. They recommend instead stoolbox --plot wavefunction --plot log, which maybe is not that bad if you provide a shorthand for --plot, like -P, but I don't know.

pfebrer avatar Aug 09 '23 14:08 pfebrer