Skip to content

Commit f05d00a

Browse files
committed
fix(__main__): show usage with no cmdline args or on error, move example file paths to argument help
1 parent 1a72f09 commit f05d00a

File tree

1 file changed

+25
-40
lines changed

1 file changed

+25
-40
lines changed

src/mcsas3/__main__.py

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -39,97 +39,82 @@ def isMac():
3939
Released under a GPLv3+ license.
4040
""",
4141
)
42-
# TODO: add info about output files to be created ...
4342
parser.add_argument(
4443
"-f",
4544
"--dataFile",
4645
type=lambda p: Path(p).absolute(),
47-
# FIXME: this default does not work for installed package
48-
default=Path(__file__).absolute().parent / "testdata" / "quickstartdemo1.csv",
49-
help="Path to the filename with the SAXS data",
50-
# required=True,
46+
help="""Path to the filename with the SAXS data, an example file can be found at
47+
'testdata/quickstartdemo1.csv' of the source distribution.""",
5148
)
5249
parser.add_argument(
5350
"-F",
5451
"--readConfigFile",
5552
type=lambda p: Path(p).absolute(),
56-
default=Path(__file__).absolute().parent
57-
/ "example_configurations"
58-
/ "read_config_csv.yaml",
59-
help="Path to the config file how to read the input data",
60-
# required=True,
53+
help="""Path to the config file how to read the input data, an example file can be found
54+
at 'example_configurations/read_config_csv.yaml' of the source distribution.""",
6155
)
56+
outpathDefault = Path().absolute() / "output.nxs"
6257
parser.add_argument(
6358
"-r",
6459
"--resultFile",
6560
type=lambda p: Path(p).absolute(),
66-
default=Path(__file__).absolute().parent / "test.nxs",
67-
help="Path to the file to create and store the McSAS3 result in",
68-
# required=True,
61+
default=outpathDefault,
62+
help=f"""Path to the file to create and store the McSAS3 result in,
63+
the default is '{outpathDefault}'.""",
6964
)
7065
parser.add_argument(
7166
"-R",
7267
"--runConfigFile",
7368
type=lambda p: Path(p).absolute(),
74-
default=Path(__file__).absolute().parent
75-
/ "example_configurations"
76-
/ "run_config_spheres_auto.yaml",
77-
help="Path to the configuration file containing the model parameters",
78-
# required=True,
69+
help="""Path to the configuration file containing the model parameters, an example file can
70+
be found at 'example_configurations/run_config_spheres_auto.yaml' of the source
71+
distribution.""",
7972
)
8073
parser.add_argument(
8174
"-H",
8275
"--histConfigFile",
8376
type=lambda p: Path(p).absolute(),
84-
default=Path("./example_configurations/hist_config_dual.yaml"),
85-
help="Path to the filename with the histogramming configuration",
86-
# required=True,
77+
help="""Path to the filename with the histogramming configuration, an example file can be
78+
found at 'example_configurations/hist_config_dual.yaml' of the source distribution.""",
8779
)
8880
parser.add_argument(
8981
"-i",
9082
"--resultIndex",
9183
type=int,
9284
default=1,
9385
help="The result index to work on, in case you want multiple McSAS runs on the same data",
94-
# required=True,
9586
)
9687
parser.add_argument(
9788
"-d",
9889
"--deleteIfExists",
99-
# type=bool,
100-
# default=False,
90+
default=False,
10191
action="store_true",
102-
help=(
103-
"Delete the output file if it exists. This will need to be activated if you are"
104-
" overwriting previous optimizations "
105-
),
106-
# required=True,
92+
help="""Delete the output file if it exists. This will need to be activated if you are
93+
overwriting previous optimizations""",
10794
)
10895
parser.add_argument(
10996
"-t",
11097
"--nThreads",
11198
type=int,
11299
default=0,
113-
help=(
114-
"The number (n>0) of cores/threads used for optimization."
115-
" If omitted, the value from the config file is used (default)."
116-
" Never more threads are used as cores exist."
117-
),
100+
help="""The number (n>0) of cores/threads used for optimization. If omitted, the value from
101+
the config file is used (default). Never more threads are used as cores exist.""",
118102
)
119103
if isMac():
120104
# on OSX remove automatically provided PID,
121105
# otherwise argparse exits and the bundle start fails silently
122106
for i in range(len(sys.argv)):
123107
if sys.argv[i].startswith("-psn"): # PID provided by osx
124108
del sys.argv[i]
125-
try:
126-
args = parser.parse_args()
127-
except SystemExit:
128-
raise
109+
args = parser.parse_args()
129110
# initiate logging (to console stdout for now)
130111
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
131112
# replaceStdOutErr() # replace all text output with our sinks
132113

133114
adict = vars(args)
134-
m = McSAS3_cli_optimize(**adict)
135-
m = McSAS3_cli_histogram(**adict)
115+
try:
116+
m = McSAS3_cli_optimize(**adict)
117+
m = McSAS3_cli_histogram(**adict)
118+
except TypeError as e: # for wrong cmdline arguments supplied
119+
print(f"ERROR: {str(e)}\n", file=sys.stderr)
120+
parser.print_help()

0 commit comments

Comments
 (0)