-
-
Notifications
You must be signed in to change notification settings - Fork 60
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
WIP: Converting sisl_toolbox
to typer.
#608
base: main
Are you sure you want to change the base?
Conversation
I have had a quick glance here.
It just somehow feels a bit clunky to me, and I am afraid typer needs more maintainers to be sustainable... :( I am thus a bit on the fence here... :( |
|
My 2nd worry is that the sgeom test.fdf --tile 2 2 --remove 2
sgeom test.fdf --remove 2 --tile 2 2 Can typer/click do this? If not, I am even more hesitant.. :( And the current PR test, does not use the subcommand arguments, could you make this work? E.g. I had a look here: https://click.palletsprojects.com/en/8.1.x/advanced/#callback-evaluation-order
would be equivalent to
:( |
I think you can parse the options by order, but I don't know how to do it yet (I don't have time to look at it now).
|
This is how it is implement it now with the |
In
yeah, ok.
Ok, that was not clear to me ;) So for now the current blocker is the order of execution. Once that is cleared, we may consider this. :) |
You basically define them, but just in code that the user has no way to know how it works :) And you can have a different parser for very similar situations. So if you want to make it clear you need to document each argument. The point of using typehints for that is to make it more clear by pairing one typehint to one parser. So for example
Ok, so you don't want to have a By the way, what might be a blocker is that the way you have |
I can't really figure this out. The idea of the toolboxes is that they are allowing external users to contribute toolboxes. And easily hook into the I don't think using typer in toolboxes, but argparse in sisl is a good idea. Everything should be one CLI. I am not set in stone on
or similar. But it would require substantial changes, and this is something I am a bit worried about... I am not saying the current way is good, or better than your proposal. I am merely stating the effort to change is significant, with (IMHO) little gain. I have just tried to play a bit with
I agree that the way things look in typer is better, but I am truly afraid of the transitioning state, and the limited gains. We could equally annotate the current sisl/src/sisl_toolbox/siesta/atom/_atom.py Line 776 in f845e95
I am also worried about As it is, it is too much work... :( |
For the record I tried something like this to get it to some state where I liked the help function: diff --git a/src/sisl_toolbox/siesta/atom/_atom.py b/src/sisl_toolbox/siesta/atom/_atom.py
index d7b24ec2c..f77932f37 100644
--- a/src/sisl_toolbox/siesta/atom/_atom.py
+++ b/src/sisl_toolbox/siesta/atom/_atom.py
@@ -750,9 +750,25 @@ class AtomPlotOption(Enum):
log = 'log'
potential = 'potential'
+from typing import Union
+import click, typer
+#InputFile = TypeVar("File", str, Path)
+
+class InputFile:
+ def __init__(self, value: Union[str, Path]):
+ self.value = value
+ def __str__(self):
+ return f"<{self.__class__.__name__}: value={self.value}>"
+
+ @classmethod
+ def parse(cls, value):
+ """Parse argument to an input file"""
+ return cls(value)
+
def atom_plot(
plot: Annotated[Optional[List[AtomPlotOption]], CLIArgument()] = None,
- input: str = "INP",
+ #input: str = "INP",
+ input: Annotated[InputFile, CLIOption(parser=InputFile.parse)] = "INP",
#plot: Optional[List[str]] = None,
l: str = 'spdf',
save: Annotated[Optional[str], CLIOption("-S", "--save")] = None,
@@ -775,7 +791,7 @@ def atom_plot(
"""
import matplotlib.pyplot as plt
- input_path = Path(input)
+ input_path = Path(input.value)
atom = AtomInput.from_input(input_path)
# If the specified input is a file, use the parent You'll note that the |
I remember using
I don't understand why, since the code for them is clearly separated. I have my basis optimizer CLI ready, but I'm hesitant to add it to the toolboxes with
Yeah, I just look at the source code. That is the problem if the whole API isn't in the documentation ;) But you only need these advanced arguments once, the users are most likely not going to need them.
The point was to move to another framework if it's better. Then the code is not full of
This could easily be fixed.
Because the |
Yes, it also seems to me that he should delegate some mainteinance work to other people and accept more contributions from the community, because there are a bunch of useful PR that are just lying there without any comment on whether they will be accepted or not. It is a bit frustrating. But I think as it is now it is useful enough for the toolbox CLI, which needs something to make it more attractive to users. |
Ok, lets try things out. Would you mind having a go at translating them to |
The toolbox you mean? |
yes. :) Only |
Ok 👍 |
I tried to be as faithful to the previous CLI as possible, but I had to change
or
if there is more than one electrode. |
if len(dict_annotation_args) == 2: | ||
try: | ||
argument_kwargs["metavar"] = f"YAML_DICT[{dict_annotation_args[0].__name__}: {dict_annotation_args[1].__name__}]" | ||
except: |
Check notice
Code scanning / CodeQL
Except block handles 'BaseException' Note
argument_kwargs = _CUSTOM_TYPE_KWARGS.get(type_, {}) | ||
if callable(argument_kwargs): | ||
argument_kwargs = argument_kwargs(args) | ||
except: |
Check notice
Code scanning / CodeQL
Except block handles 'BaseException' Note
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: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
After
How it works
While working out the details, I found some advantages and disadvantages:
Advantages
Geometry
,AtomsArgument
) that all CLIs would use.Disadvantages
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 insteadstoolbox --plot wavefunction --plot log
, which maybe is not that bad if you provide a shorthand for--plot
, like-P
, but I don't know.