Skip to content

Commit

Permalink
suggest alternatives upon invalid command line arguments with leading -
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Nov 18, 2013
1 parent a735e3a commit fe8ed03
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
14 changes: 13 additions & 1 deletion main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1240,9 +1240,21 @@ try
),": print help for all compiler metadatas");
] in
let args_callback cl = classes := make_path cl :: !classes in
let all_args_spec = basic_args_spec @ adv_args_spec in
let process args =
let current = ref 0 in
Arg.parse_argv ~current (Array.of_list ("" :: List.map expand_env args)) (basic_args_spec @ adv_args_spec) args_callback usage
try
Arg.parse_argv ~current (Array.of_list ("" :: List.map expand_env args)) all_args_spec args_callback usage
with (Arg.Bad msg) as exc ->
let r = Str.regexp "unknown option `\\([-A-Za-z]+\\)'" in
try
ignore(Str.search_forward r msg 0);
let s = Str.matched_group 1 msg in
let sl = List.map (fun (s,_,_) -> s) all_args_spec in
let msg = Typecore.string_error_raise s sl (Printf.sprintf "Invalid command: %s" s) in
raise (Arg.Bad msg)
with Not_found ->
raise exc
in
process_ref := process;
process ctx.com.args;
Expand Down
8 changes: 6 additions & 2 deletions typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ let levenshtein a b =
done;
matrix.(m).(n)

let string_error s sl msg =
let string_error_raise s sl msg =
if sl = [] then msg else
let cl = List.map (fun s2 -> s2,levenshtein s s2) sl in
let cl = List.sort (fun (_,c1) (_,c2) -> compare c1 c2) cl in
Expand All @@ -184,10 +184,14 @@ let string_error s sl msg =
| _ -> []
in
match loop cl with
| [] -> msg
| [] -> raise Not_found
| [s] -> Printf.sprintf "%s (Suggestion: %s)" msg s
| sl -> Printf.sprintf "%s (Suggestions: %s)" msg (String.concat ", " sl)

let string_error s sl msg =
try string_error_raise s sl msg
with Not_found -> msg

let string_source t = match follow t with
| TInst(c,_) -> List.map (fun cf -> cf.cf_name) c.cl_ordered_fields
| TEnum(en,_) -> en.e_names
Expand Down

0 comments on commit fe8ed03

Please sign in to comment.