Skip to content

Commit ab76610

Browse files
new: use the filetype detector to infer what to do in case of multiple implicit arguments from the cli
Signed-off-by: thiswillbeyourgithub <[email protected]>
1 parent 05966c6 commit ab76610

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

wdoc/__main__.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from wdoc.utils.env import env, is_out_piped
2626
from wdoc.utils.typechecker import optional_typecheck
2727
from wdoc.utils.misc import get_piped_input, tasks_list
28+
from wdoc.utils.batch_file_loader import infer_filetype, NoInferrableFiletype
2829
from typing import Tuple, List, Dict, Any
2930
import io
3031

@@ -251,22 +252,36 @@ def cli_launcher() -> None:
251252
args.append(new_arg)
252253
sys.argv.append(new_arg) # Append the new argument
253254

254-
# if no --path but an arg: use it as path arg
255-
if not ("path" in args or "path" in kwargs):
256-
if len(args) == 1:
257-
sys.argv.remove(args[0])
258-
sys.argv.append(f"--path={args[0]}")
259-
logger.debug(f"Set the argument '{args[0]}' to a --path argument")
255+
# if there are remaining args, use the infer_filetype function to see if they are the missing path or the query
256+
if args:
257+
candidates = []
258+
for arg in args:
259+
# if we can't infer the filetype then it's probably the implicit --query of the user
260+
infered = None
261+
try:
262+
infered = infer_filetype(arg)
263+
except NoInferrableFiletype:
264+
if "query" in kwargs["task"]:
265+
infered = "user_query"
266+
candidates.append(infered)
267+
268+
if "query" in kwargs["task"] and (
269+
"query" not in kwargs or not isinstance(kwargs["query"], str)
270+
):
271+
if len([c for c in candidates if c == "user_query"]) == 1:
272+
query_index = candidates.index("user_query")
273+
kwargs["query"] = args.pop(query_index)
274+
sys.argv.append(f"--query='{kwargs['query']}'")
275+
candidates.pop(query_index)
276+
277+
if (
278+
args
279+
and not ("path" in args or "path" in kwargs)
280+
and len(candidates) == 1
281+
and candidates[0]
282+
):
260283
kwargs["path"] = args.pop(0)
261-
262-
# if --path is empty give it the remaining arg
263-
if ("path" not in kwargs or isinstance(kwargs["path"], (bool, type(None)))) and len(
264-
args
265-
) == 1:
266-
sys.argv.remove(args[0])
267-
sys.argv.append(f"--path={args[0]}")
268-
logger.debug(f"Set the argument '{args[0]}' to a --path argument")
269-
kwargs["path"] = args.pop(0)
284+
sys.argv.append(f"--path='{kwargs['path']}'")
270285

271286
# if args is not empty, we have not succesfully parsed everything
272287
if args:

0 commit comments

Comments
 (0)