Skip to content

Commit 9bfc039

Browse files
committed
adding handle function for dataset CLI argument
1 parent 01c49a2 commit 9bfc039

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

servicex_analysis_utils/cli.py

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,20 @@
66
from .file_peeking import get_structure
77

88

9-
def run_from_command():
10-
parser = argparse.ArgumentParser(
11-
description="CLI tool for retrieving ROOT file structures."
12-
)
9+
def make_dataset_list(dataset_arg):
10+
"""
11+
Helper to handle the user input daset argument.
12+
Loads to dict if input is .json else returns default input
13+
Output is given to get_structure()
1314
14-
parser.add_argument(
15-
"dataset",
16-
nargs="+",
17-
help="Input datasets (Rucio DID) or a JSON file containing datasets in a dict.",
18-
)
19-
parser.add_argument(
20-
"--filter-branch",
21-
default="",
22-
help="Only display branches containing this string.",
23-
)
15+
Parameters:
16+
dataset_arg (str, [str]): Single DS identifier, list of multiple identifiers or path/to/.json containig identifiers and sample names.
2417
25-
args = parser.parse_args()
26-
27-
if len(args.dataset) == 1 and args.dataset[0].endswith(".json"):
28-
dataset_file = args.dataset[0]
18+
Returns:
19+
dataset (str, [str], dict): dictionary loaded from the json
20+
"""
21+
if len(dataset_arg) == 1 and dataset_arg[0].endswith(".json"):
22+
dataset_file = dataset_arg[0]
2923

3024
if not os.path.isfile(dataset_file):
3125
logging.error(f"Error: JSON file '{dataset_file}' not found.")
@@ -46,9 +40,36 @@ def run_from_command():
4640
sys.exit(1)
4741

4842
else:
49-
# If dataset is provided directly in CLI, use it as a list
50-
dataset = args.dataset
43+
# If DS is provided in CLI instead of json, use it as a list (default)
44+
dataset = dataset_arg
45+
46+
return dataset
47+
48+
49+
def run_from_command():
50+
"""
51+
Calls the get_structure function and sends results to stdout.
52+
To run on command line: servicex-get-structure -dataset --fileter-branch
53+
"""
54+
parser = argparse.ArgumentParser(
55+
description="CLI tool for retrieving ROOT file structures."
56+
)
57+
58+
parser.add_argument(
59+
"dataset",
60+
nargs="+",
61+
help="Input datasets (Rucio DID) or a JSON file containing datasets in a dict.",
62+
)
63+
parser.add_argument(
64+
"--filter-branch",
65+
default="",
66+
help="Only display branches containing this string.",
67+
)
68+
69+
args = parser.parse_args()
70+
71+
ds_format = make_dataset_list(args.dataset)
5172

52-
result = get_structure(dataset, filter_branch=args.filter_branch, do_print=False)
73+
result = get_structure(ds_format, filter_branch=args.filter_branch, do_print=False)
5374

5475
print(result)

0 commit comments

Comments
 (0)