Skip to content

Commit 806c78e

Browse files
authored
Merge pull request #908 from pfebrer/rich_cli
Formatting the argparse CLIs
2 parents 074d362 + f9fb352 commit 806c78e

File tree

7 files changed

+30
-11
lines changed

7 files changed

+30
-11
lines changed

src/sisl/_core/geometry.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
list_index_le,
4646
)
4747
from sisl._internal import set_module
48+
from sisl._lib._argparse import SislHelpFormatter
4849
from sisl._math_small import cross3, is_ascending, xyz_to_spherical_cos_phi
4950
from sisl._namedindex import NamedIndex
5051
from sisl.messages import SislError, deprecate_argument, deprecation, info, warn
@@ -4783,7 +4784,7 @@ def sgeom(geometry=None, argv=None, ret_geometry=False):
47834784

47844785
p = argparse.ArgumentParser(
47854786
exe,
4786-
formatter_class=argparse.RawDescriptionHelpFormatter,
4787+
formatter_class=SislHelpFormatter,
47874788
description=description,
47884789
)
47894790

src/sisl/_core/grid.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from sisl._dispatcher import AbstractDispatch, ClassDispatcher, TypeDispatcher
2121
from sisl._help import dtype_complex_to_real, wrap_filterwarnings
2222
from sisl._internal import set_module
23+
from sisl._lib._argparse import SislHelpFormatter
2324
from sisl.messages import deprecate_argument, deprecation
2425
from sisl.shape import Shape
2526
from sisl.utils import (
@@ -1747,7 +1748,7 @@ def sgrid(grid=None, argv=None, ret_grid=False):
17471748

17481749
p = argparse.ArgumentParser(
17491750
exe,
1750-
formatter_class=argparse.RawDescriptionHelpFormatter,
1751+
formatter_class=SislHelpFormatter,
17511752
description=description,
17521753
)
17531754

src/sisl/_lib/_argparse.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This Source Code Form is subject to the terms of the Mozilla Public
2+
# License, v. 2.0. If a copy of the MPL was not distributed with this
3+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
try:
5+
from rich_argparse import RichHelpFormatter
6+
7+
SislHelpFormatter = RichHelpFormatter
8+
except ImportError:
9+
import argparse
10+
11+
SislHelpFormatter = argparse.RawDescriptionHelpFormatter

src/sisl/utils/_sisl_cmd.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"""
1010
import argparse
1111

12+
from sisl._lib._argparse import SislHelpFormatter
13+
1214
__all__ = ["sisl_cmd"]
1315

1416

@@ -99,7 +101,7 @@ def sisl_cmd(argv=None, sile=None):
99101

100102
p = argparse.ArgumentParser(
101103
exe,
102-
formatter_class=argparse.RawDescriptionHelpFormatter,
104+
formatter_class=SislHelpFormatter,
103105
description=description,
104106
conflict_handler="resolve",
105107
)

src/sisl_toolbox/cli/_cli.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from collections.abc import Callable
1515
from pathlib import Path
1616

17+
from sisl._lib._argparse import SislHelpFormatter
18+
1719

1820
class SToolBoxCLI:
1921
"""Run the CLI `stoolbox`"""
@@ -47,7 +49,9 @@ def __call__(self, argv=None):
4749
# Create command-line
4850
cmd = Path(sys.argv[0])
4951
p = argparse.ArgumentParser(
50-
f"{cmd.name}", description="Specific toolboxes to aid sisl users"
52+
f"{cmd.name}",
53+
description="Specific toolboxes to aid sisl users",
54+
formatter_class=SislHelpFormatter,
5155
)
5256

5357
info = {
@@ -64,7 +68,7 @@ def __call__(self, argv=None):
6468
subp = p.add_subparsers(**info)
6569

6670
for cmd in self._cmds:
67-
cmd(subp)
71+
cmd(subp, parser_kwargs=dict(formatter_class=p.formatter_class))
6872

6973
args = p.parse_args(argv)
7074
args.runner(args)

src/sisl_toolbox/siesta/atom/_atom.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ def next_rc(ir, ic, nrows, ncols):
803803
return fig, axs
804804

805805

806-
def atom_plot_cli(subp=None):
806+
def atom_plot_cli(subp=None, parser_kwargs={}):
807807
"""Run plotting command for the output of atom"""
808808

809809
is_sub = not subp is None
@@ -812,11 +812,11 @@ def atom_plot_cli(subp=None):
812812
if is_sub:
813813
global _script
814814
_script = f"{_script} atom-plot"
815-
p = subp.add_parser("atom-plot", description=title, help=title)
815+
p = subp.add_parser("atom-plot", description=title, help=title, **parser_kwargs)
816816
else:
817817
import argparse
818818

819-
p = argparse.ArgumentParser(title)
819+
p = argparse.ArgumentParser(title, **parser_kwargs)
820820

821821
p.add_argument(
822822
"--plot",

src/sisl_toolbox/transiesta/poisson/fftpoisson_fix.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,16 +344,16 @@ def sl2idx(grid, sl):
344344
return grid
345345

346346

347-
def fftpoisson_fix_cli(subp=None):
347+
def fftpoisson_fix_cli(subp=None, parser_kwargs={}):
348348
is_sub = not subp is None
349349

350350
title = "FFT Poisson corrections for TranSiesta calculations for arbitrary number of electrodes."
351351
if is_sub:
352352
global _script
353353
_script = f"{_script} ts-fft"
354-
p = subp.add_parser("ts-fft", description=title, help=title)
354+
p = subp.add_parser("ts-fft", description=title, help=title, **parser_kwargs)
355355
else:
356-
p = argp.ArgumentParser(title)
356+
p = argp.ArgumentParser(title, **parser_kwargs)
357357

358358
tuning = p.add_argument_group(
359359
"tuning", "Tuning fine details of the Poisson calculation."

0 commit comments

Comments
 (0)