Skip to content

Commit 0aad27c

Browse files
committed
rimport: Modernize help.
1 parent d990f97 commit 0aad27c

3 files changed

Lines changed: 24 additions & 65 deletions

File tree

rimport

Lines changed: 18 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,9 @@
22
# TODO: Move all the Python into new file rimport.py for simpler testing. Keep rimport as a
33
# convenience wrapper.
44
"""
5-
A drop-in CLI replacement for the legacy `rimport` csh tool.
6-
7-
This script preserves the original command-line interface:
8-
9-
SYNOPSIS
10-
rimport [-file filename] [-list filelist] [-inputdata inputdata_dir] [-help]
11-
12-
OPTIONS
13-
-file filename
14-
provide a single filename relative to the top inputdata directory
15-
-list filelist
16-
provide a file that contains a list of filenames to import. all filenames
17-
in the list are relative to the top inputdata area.
18-
-inputdata inputdata_dir
19-
change the default local top level inputdata directory
20-
-help
21-
get help about this tool
22-
23-
Customize the `do_new_action(path)` function to implement the new behavior.
24-
By default, it prints which files would be processed.
5+
Copy files from CESM inputdata directory to a publishing directory.
6+
7+
Do `rimport --help` for more information.
258
"""
269
from __future__ import annotations
2710

@@ -34,43 +17,27 @@ from pathlib import Path
3417
from typing import Iterable, List
3518

3619

37-
class PlainHelpFormatter(argparse.RawTextHelpFormatter):
38-
pass
39-
40-
4120
def build_parser() -> argparse.ArgumentParser:
42-
synopsis = (
43-
"rimport [-file filename] [-list filelist] [-inputdata inputdata_dir] [-help]"
44-
)
45-
46-
description = (
47-
"SYNOPSIS\n"
48-
f" {synopsis}\n\n"
49-
"OPTIONS\n"
50-
" -file filename\n"
51-
" provide a single filename relative to the top inputdata directory\n"
52-
" -list filelist\n"
53-
" provide a file that contains a list of filenames to import. all filenames\n"
54-
" in the list are relative to the top inputdata area.\n"
55-
" -inputdata inputdata_dir\n"
56-
" change the default local top level inputdata directory\n"
57-
" -help\n"
58-
" get help about this tool\n"
59-
)
60-
6121
parser = argparse.ArgumentParser(
62-
prog="rimport",
63-
description=description,
64-
formatter_class=PlainHelpFormatter,
65-
add_help=False, # preserve original -help only
66-
usage=synopsis,
22+
description="Copy files from CESM inputdata directory to a publishing directory."
6723
)
6824

6925
# Mutually exclusive: -file or -list (one required)
7026
group = parser.add_mutually_exclusive_group(required=True)
71-
group.add_argument("-file", dest="file", metavar="filename", help=argparse.SUPPRESS)
7227
group.add_argument(
73-
"-list", dest="filelist", metavar="filelist", help=argparse.SUPPRESS
28+
"-file",
29+
dest="file",
30+
metavar="filename",
31+
help="Provide a single filename relative to the top inputdata directory",
32+
)
33+
group.add_argument(
34+
"-list",
35+
dest="filelist",
36+
metavar="filelist",
37+
help=(
38+
"Provide a file that contains a list of filenames to import. All filenames in the list"
39+
"are relative to the top inputdata area."
40+
),
7441
)
7542

7643
parser.add_argument(
@@ -80,12 +47,9 @@ def build_parser() -> argparse.ArgumentParser:
8047
default=os.path.join(
8148
"/glade", "campaign", "cesm", "cesmdata", "cseg", "inputdata"
8249
),
83-
help=argparse.SUPPRESS,
50+
help="Change the default local top level inputdata directory.",
8451
)
8552

86-
# Provide -help to mirror legacy behavior
87-
parser.add_argument("-help", action="help", help=argparse.SUPPRESS)
88-
8953
return parser
9054

9155

tests/rimport/test_build_parser.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ def test_parser_creation(self):
3232
parser = rimport.build_parser()
3333
assert isinstance(parser, argparse.ArgumentParser)
3434

35-
def test_parser_prog_name(self):
36-
"""Test that parser has correct program name."""
37-
parser = rimport.build_parser()
38-
assert parser.prog == "rimport"
39-
4035
def test_file_argument_accepted(self):
4136
"""Test that -file argument is accepted."""
4237
parser = rimport.build_parser()
@@ -71,7 +66,7 @@ def test_file_or_list_required(self, capsys):
7166
# Check that the error message explains the problem
7267
captured = capsys.readouterr()
7368
stderr_lines = captured.err.strip().split("\n")
74-
assert "rimport: error: one of the arguments" in stderr_lines[-1]
69+
assert "error: one of the arguments" in stderr_lines[-1]
7570

7671
def test_inputdata_default(self):
7772
"""Test that -inputdata has correct default value."""
@@ -89,9 +84,9 @@ def test_inputdata_custom(self):
8984
args = parser.parse_args(["-file", "test.txt", "-inputdata", custom_path])
9085
assert args.inputdata == custom_path
9186

92-
@pytest.mark.parametrize("help_flag", ["-help", "-h"])
87+
@pytest.mark.parametrize("help_flag", ["-help", "-h", "--help"])
9388
def test_help_flags_show_help(self, help_flag):
94-
"""Test that -help and -h flags trigger help."""
89+
"""Test that all help flag options trigger help."""
9590
parser = rimport.build_parser()
9691
with pytest.raises(SystemExit) as exc_info:
9792
parser.parse_args([help_flag])

tests/rimport/test_cmdline.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def test_error_for_empty_list_file(self, rimport_script, test_env, rimport_env):
249249
assert result.returncode == 2
250250
assert "no filenames found" in result.stderr
251251

252-
@pytest.mark.parametrize("help_flag", ["-help", "-h"])
252+
@pytest.mark.parametrize("help_flag", ["-help", "-h", "--help"])
253253
def test_help_flag_shows_help(self, rimport_script, help_flag):
254254
"""Test that help flags show help message."""
255255
command = [sys.executable, rimport_script, help_flag]
@@ -263,8 +263,8 @@ def test_help_flag_shows_help(self, rimport_script, help_flag):
263263

264264
# Help should exit with code 0
265265
assert result.returncode == 0
266-
assert "SYNOPSIS" in result.stdout
267-
assert "OPTIONS" in result.stdout
266+
assert "usage:" in result.stdout
267+
assert "options:" in result.stdout
268268

269269
def test_list_with_comments_and_blanks(self, rimport_script, test_env, rimport_env):
270270
"""Test that list file with comments and blank lines works correctly."""

0 commit comments

Comments
 (0)