Skip to content

Commit 1960c4d

Browse files
authored
Merge pull request #437 from dbic/ux-ensure-heuristic
ENH: Provide informative exception if command needs a heuristic to be specified
2 parents 8487d1d + d706d59 commit 1960c4d

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

heudiconv/cli/run.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def process_extra_commands(outdir, args):
6262
for f in args.files:
6363
treat_infofile(f)
6464
elif args.command == 'ls':
65+
ensure_heuristic_arg(args)
6566
heuristic = load_heuristic(args.heuristic)
6667
heuristic_ls = getattr(heuristic, 'ls', None)
6768
for f in args.files:
@@ -78,6 +79,7 @@ def process_extra_commands(outdir, args):
7879
% (str(study_session), len(sequences), suf)
7980
)
8081
elif args.command == 'populate-templates':
82+
ensure_heuristic_arg(args)
8183
heuristic = load_heuristic(args.heuristic)
8284
for f in args.files:
8385
populate_bids_templates(f, getattr(heuristic, 'DEFAULT_FIELDS', {}))
@@ -88,16 +90,21 @@ def process_extra_commands(outdir, args):
8890
for name_desc in get_known_heuristics_with_descriptions().items():
8991
print("- %s: %s" % name_desc)
9092
elif args.command == 'heuristic-info':
91-
from ..utils import get_heuristic_description, get_known_heuristic_names
92-
if not args.heuristic:
93-
raise ValueError("Specify heuristic using -f. Known are: %s"
94-
% ', '.join(get_known_heuristic_names()))
93+
ensure_heuristic_arg(args)
94+
from ..utils import get_heuristic_description
9595
print(get_heuristic_description(args.heuristic, full=True))
9696
else:
9797
raise ValueError("Unknown command %s", args.command)
9898
return
9999

100100

101+
def ensure_heuristic_arg(args):
102+
from ..utils import get_known_heuristic_names
103+
if not args.heuristic:
104+
raise ValueError("Specify heuristic using -f. Known are: %s"
105+
% ', '.join(get_known_heuristic_names()))
106+
107+
101108
def main(argv=None):
102109
parser = get_parser()
103110
args = parser.parse_args(argv)

heudiconv/tests/test_main.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ def test_populate_bids_templates(tmpdir):
6565

6666
# it should also be available as a command
6767
os.unlink(str(description_file))
68+
69+
# it must fail if no heuristic was provided
70+
with pytest.raises(ValueError) as cme:
71+
runner([
72+
'--command', 'populate-templates',
73+
'--files', str(tmpdir)
74+
])
75+
assert str(cme.value).startswith("Specify heuristic using -f. Known are:")
76+
assert "convertall," in str(cme.value)
77+
assert not description_file.exists()
78+
6879
runner([
6980
'--command', 'populate-templates', '-f', 'convertall',
7081
'--files', str(tmpdir)

0 commit comments

Comments
 (0)