Skip to content

Commit a247ec6

Browse files
committed
feat: migration main() to argparse
1 parent 7170b07 commit a247ec6

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/diffpy/cmi/app.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import getopt
1+
import argparse
22
import sys
33

44
from diffpy.cmi.version import __version__
@@ -43,22 +43,31 @@ def print_version():
4343

4444

4545
def main():
46-
try:
47-
opts, args = getopt.gnu_getopt(sys.argv[1:], "h", ["help", "version"])
48-
except getopt.GetoptError as err:
49-
print(f"Error: {err}", file=sys.stderr)
46+
parser = argparse.ArgumentParser(
47+
prog="diffpy-cmi",
48+
add_help=False,
49+
)
50+
parser.add_argument(
51+
"--version", action="store_true", help="Show version and exit"
52+
)
53+
parser.add_argument(
54+
"-h", "--help", action="store_true", help="Show this message and exit"
55+
)
56+
args, unknown = parser.parse_known_args()
57+
if unknown:
58+
print(
59+
f"Error: unrecognized arguments: {' '.join(unknown)}",
60+
file=sys.stderr,
61+
)
5062
short_usage()
5163
sys.exit(1)
52-
53-
for opt, _ in opts:
54-
if opt in ("-h", "--help"):
55-
usage()
56-
return
57-
elif opt == "--version":
58-
print_version()
59-
return
60-
61-
# Default behavior (if no arguments)
64+
if args.help:
65+
usage()
66+
return
67+
if args.version:
68+
print_version()
69+
return
70+
# Default behavior (no args)
6271
usage()
6372

6473

0 commit comments

Comments
 (0)