Skip to content

Commit fc7db02

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 049b6c2 commit fc7db02

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

misc-tools/configure-domjudge.in

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,17 @@ under the GNU GPL. See README and COPYING for details.
1414
'''
1515

1616
import json
17-
import os.path
17+
import os
1818
import requests
1919
import requests.utils
2020
import shutil
2121
import sys
22+
from argparse import ArgumentParser
2223
from typing import List, Set
2324

2425
sys.path.append('@domserver_libdir@')
2526
import dj_utils
2627

27-
def usage():
28-
print(f'Usage: {sys.argv[0]} [<domjudge-api-url>]')
29-
exit(1)
30-
31-
3228
def compare_configs(expected_config: Set, actual_config: Set, num_spaces=4, key_mismatch_in_diff=False) -> (List, Set, Set):
3329
diffs = []
3430
space_string = ' ' * num_spaces
@@ -55,10 +51,16 @@ def _keyify_list(l: List) -> Set:
5551
return { elem['id']: elem for elem in l }
5652

5753

58-
if len(sys.argv) == 2:
59-
dj_utils.domjudge_api_url = sys.argv[1]
60-
elif len(sys.argv) != 1:
61-
usage()
54+
parser = ArgumentParser(description='Configure DOMjudge via the API.')
55+
parser.add_argument('-d', '--dir', help="directory to read configuration from, defaults to current directory")
56+
parser.add_argument('-u', '--url', help="DOMjudge API URL to use, if not specified use the CLI interface")
57+
args = parser.parse_args()
58+
59+
if args.dir:
60+
os.chdir(args.dir)
61+
62+
if args.url:
63+
dj_utils.domjudge_api_url = args.url
6264

6365
user_data = dj_utils.do_api_request('user')
6466
if 'admin' not in user_data['roles']:

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)