Skip to content

Commit 2c3d504

Browse files
committed
Use ArgumentParser and make arguments optional
This gives more flexibility for extension in the future and leaves the default usage without arguments unchanged.
1 parent 7ffdd9b commit 2c3d504

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

misc-tools/import-contest.in

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ Part of the DOMjudge Programming Contest Jury System and licensed
1515
under the GNU GPL. See README and COPYING for details.
1616
'''
1717

18-
from os import listdir
18+
from argparse import ArgumentParser
19+
from os import chdir, listdir
1920
from typing import List
2021
import json
2122
import os.path
@@ -30,11 +31,15 @@ import dj_utils
3031

3132
cid = None
3233

34+
parser = ArgumentParser(description='Import a contest archive to DOMjudge via the API.')
35+
parser.add_argument('-d', '--dir', help="directory containing the contest archive, defaults to current directory")
36+
parser.add_argument('-u', '--url', help="DOMjudge API URL to use, if not specified use the CLI interface")
37+
args = parser.parse_args()
3338

34-
def usage():
35-
print(f'Usage: {sys.argv[0]} [<domjudge-api-url>]')
36-
exit(1)
37-
39+
if args.dir:
40+
chdir(args.dir)
41+
if args.url:
42+
dj_utils.domjudge_api_url = args.url
3843

3944
def import_file(entity: str, files: List[str]) -> bool:
4045
any_matched = False
@@ -133,11 +138,6 @@ def import_contest_problemset_document(cid: str):
133138
else:
134139
print('Skipping contest problemset import.')
135140

136-
if len(sys.argv) == 2:
137-
dj_utils.domjudge_api_url = sys.argv[1]
138-
elif len(sys.argv) != 1:
139-
usage()
140-
141141
user_data = dj_utils.do_api_request('user')
142142
if 'admin' not in user_data['roles']:
143143
print('Your user does not have the \'admin\' role, can not import.')

0 commit comments

Comments
 (0)