You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

514 lines
21 KiB

  1. %%%-------------------------------------------------------------------
  2. %%% @author Juan Jose Comellas <juanjo@comellas.org>
  3. %%% @copyright (C) 2009 Juan Jose Comellas
  4. %%% @doc Parses command line options with a format similar to that of GNU getopt.
  5. %%% @end
  6. %%%
  7. %%% This source file is subject to the New BSD License. You should have received
  8. %%% a copy of the New BSD license with this software. If not, it can be
  9. %%% retrieved from: http://www.opensource.org/licenses/bsd-license.php
  10. %%%-------------------------------------------------------------------
  11. -module(getopt).
  12. -author('juanjo@comellas.org').
  13. -export([parse/2, usage/2, usage/3, usage/4]).
  14. -define(TAB_LENGTH, 8).
  15. %% Indentation of the help messages in number of tabs.
  16. -define(INDENTATION, 3).
  17. %% Position of each field in the option specification tuple.
  18. -define(OPT_NAME, 1).
  19. -define(OPT_SHORT, 2).
  20. -define(OPT_LONG, 3).
  21. -define(OPT_ARG, 4).
  22. -define(OPT_HELP, 5).
  23. -define(IS_OPT_SPEC(Opt), (is_tuple(Opt) andalso (size(Opt) =:= ?OPT_HELP))).
  24. %% @type arg_type() = 'atom' | 'binary' | 'bool' | 'float' | 'integer' | 'string'.
  25. %% Atom indicating the data type that an argument can be converted to.
  26. -type arg_type() :: 'atom' | 'binary' | 'boolean' | 'float' | 'integer' | 'string'.
  27. %% @type arg_value() = atom() | binary() | bool() | float() | integer() | string().
  28. %% Data type that an argument can be converted to.
  29. -type arg_value() :: atom() | binary() | boolean() | float() | integer() | string().
  30. %% @type arg_spec() = arg_type() | {arg_type(), arg_value()} | undefined.
  31. %% Argument specification.
  32. -type arg_spec() :: arg_type() | {arg_type(), arg_value()} | undefined.
  33. %% @type option() = atom() | {atom(), arg_value()}. Option type and optional default argument.
  34. -type option() :: atom() | {atom(), arg_value()}.
  35. %% @type option_spec() = #option{}. Command line option specification.
  36. -type option_spec() :: {
  37. Name :: atom(),
  38. Short :: char() | undefined,
  39. Long :: string() | undefined,
  40. ArgSpec :: arg_spec(),
  41. Help :: string() | undefined
  42. }.
  43. -spec parse([option_spec()], string() | [string()]) -> {ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data :: any()}}.
  44. %%--------------------------------------------------------------------
  45. %% @spec parse(OptSpecList::[option_spec()], Args::string() | [string()]) -> [option()].
  46. %% @doc Parse the command line options and arguments returning a list of tuples
  47. %% and/or atoms using the Erlang convention for sending options to a
  48. %% function.
  49. %%--------------------------------------------------------------------
  50. parse(OptSpecList, CmdLine) ->
  51. try
  52. Args = if
  53. is_integer(hd(CmdLine)) ->
  54. string:tokens(CmdLine, " \t\n");
  55. true ->
  56. CmdLine
  57. end,
  58. parse(OptSpecList, [], [], 0, Args)
  59. catch
  60. throw: {error, {_Reason, _Data}} = Error ->
  61. Error
  62. end.
  63. -spec parse([option_spec()], [option()], [string()], integer(), [string()]) ->
  64. {ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data:: any()}}.
  65. %% Process the option terminator.
  66. parse(OptSpecList, OptAcc, ArgAcc, _ArgPos, ["--" | Tail]) ->
  67. % Any argument present after the terminator is not considered an option.
  68. {ok, {lists:reverse(append_default_options(OptSpecList, OptAcc)), lists:reverse(ArgAcc, Tail)}};
  69. %% Process long options.
  70. parse(OptSpecList, OptAcc, ArgAcc, ArgPos, [[$-, $- | OptArg] = OptStr | Tail]) ->
  71. parse_option_long(OptSpecList, OptAcc, ArgAcc, ArgPos, Tail, OptStr, OptArg);
  72. %% Process short options.
  73. parse(OptSpecList, OptAcc, ArgAcc, ArgPos, [[$- | [_Char | _] = OptArg] = OptStr | Tail]) ->
  74. parse_option_short(OptSpecList, OptAcc, ArgAcc, ArgPos, Tail, OptStr, OptArg);
  75. %% Process non-option arguments.
  76. parse(OptSpecList, OptAcc, ArgAcc, ArgPos, [Arg | Tail]) ->
  77. case find_non_option_arg(OptSpecList, ArgPos) of
  78. {value, OptSpec} when ?IS_OPT_SPEC(OptSpec) ->
  79. parse(OptSpecList, [convert_option_arg(OptSpec, Arg) | OptAcc],
  80. ArgAcc, ArgPos + 1, Tail);
  81. false ->
  82. parse(OptSpecList, OptAcc, [Arg | ArgAcc], ArgPos, Tail)
  83. end;
  84. parse(OptSpecList, OptAcc, ArgAcc, _ArgPos, []) ->
  85. % Once we have completed gathering the options we add the ones that were
  86. % not present but had default arguments in the specification.
  87. {ok, {lists:reverse(append_default_options(OptSpecList, OptAcc)), lists:reverse(ArgAcc)}}.
  88. -spec parse_option_long([option_spec()], [option()], [string()], integer(), [string()], string(), string()) ->
  89. {ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data:: any()}}.
  90. %% @doc Parse a long option, add it to the option accumulator and continue
  91. %% parsing the rest of the arguments recursively.
  92. %% A long option can have the following syntax:
  93. %% --foo Single option 'foo', no argument
  94. %% --foo=bar Single option 'foo', argument "bar"
  95. %% --foo bar Single option 'foo', argument "bar"
  96. parse_option_long(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptStr, OptArg) ->
  97. case split_assigned_arg(OptArg) of
  98. {Long, Arg} ->
  99. % Get option that has its argument within the same string
  100. % separated by an equal ('=') character (e.g. "--port=1000").
  101. parse_option_assigned_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptStr, Long, Arg);
  102. Long ->
  103. case lists:keysearch(Long, ?OPT_LONG, OptSpecList) of
  104. {value, {Name, _Short, Long, undefined, _Help}} ->
  105. parse(OptSpecList, [Name | OptAcc], ArgAcc, ArgPos, Args);
  106. {value, {_Name, _Short, Long, _ArgSpec, _Help} = OptSpec} ->
  107. % The option argument string is empty, but the option requires
  108. % an argument, so we look into the next string in the list.
  109. parse_option_next_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptSpec);
  110. false ->
  111. throw({error, {invalid_option, OptStr}})
  112. end
  113. end.
  114. -spec parse_option_assigned_arg([option_spec()], [option()], [string()], integer(),
  115. [string()], string(), string(), string()) ->
  116. {ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data:: any()}}.
  117. %% @doc Parse an option where the argument is 'assigned' in the same string using
  118. %% the '=' character, add it to the option accumulator and continue parsing the
  119. %% rest of the arguments recursively. This syntax is only valid for long options.
  120. parse_option_assigned_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptStr, Long, Arg) ->
  121. case lists:keysearch(Long, ?OPT_LONG, OptSpecList) of
  122. {value, {_Name, _Short, Long, ArgSpec, _Help} = OptSpec} ->
  123. case ArgSpec of
  124. undefined ->
  125. throw({error, {invalid_option_arg, OptStr}});
  126. _ ->
  127. parse(OptSpecList, [convert_option_arg(OptSpec, Arg) | OptAcc], ArgAcc, ArgPos, Args)
  128. end;
  129. false ->
  130. throw({error, {invalid_option, OptStr}})
  131. end.
  132. -spec split_assigned_arg(string()) -> {Name :: string(), Arg :: string()} | string().
  133. %% @doc Split an option string that may contain an option with its argument
  134. %% separated by an equal ('=') character (e.g. "port=1000").
  135. split_assigned_arg(OptStr) ->
  136. split_assigned_arg(OptStr, OptStr, []).
  137. split_assigned_arg(_OptStr, [$= | Tail], Acc) ->
  138. {lists:reverse(Acc), Tail};
  139. split_assigned_arg(OptStr, [Char | Tail], Acc) ->
  140. split_assigned_arg(OptStr, Tail, [Char | Acc]);
  141. split_assigned_arg(OptStr, [], _Acc) ->
  142. OptStr.
  143. %% @doc Parse a short option, add it to the option accumulator and continue
  144. %% parsing the rest of the arguments recursively.
  145. %% A short option can have the following syntax:
  146. %% -a Single option 'a', no argument or implicit boolean argument
  147. %% -a foo Single option 'a', argument "foo"
  148. %% -afoo Single option 'a', argument "foo"
  149. %% -abc Multiple options: 'a'; 'b'; 'c'
  150. %% -bcafoo Multiple options: 'b'; 'c'; 'a' with argument "foo"
  151. -spec parse_option_short([option_spec()], [option()], [string()], integer(), [string()], string(), string()) ->
  152. {ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data:: any()}}.
  153. parse_option_short(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptStr, [Short | Arg]) ->
  154. case lists:keysearch(Short, ?OPT_SHORT, OptSpecList) of
  155. {value, {Name, Short, _Long, undefined, _Help}} ->
  156. parse_option_short(OptSpecList, [Name | OptAcc], ArgAcc, ArgPos, Args, OptStr, Arg);
  157. {value, {_Name, Short, _Long, ArgSpec, _Help} = OptSpec} ->
  158. case Arg of
  159. [] ->
  160. % The option argument string is empty, but the option requires
  161. % an argument, so we look into the next string in the list.
  162. parse_option_next_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptSpec);
  163. _ ->
  164. case is_valid_arg(ArgSpec, Arg) of
  165. true ->
  166. parse(OptSpecList, [convert_option_arg(OptSpec, Arg) | OptAcc], ArgAcc, ArgPos, Args);
  167. _ ->
  168. parse_option_short(OptSpecList, [convert_option_no_arg(OptSpec) | OptAcc], ArgAcc, ArgPos, Args, OptStr, Arg)
  169. end
  170. end;
  171. false ->
  172. throw({error, {invalid_option, OptStr}})
  173. end;
  174. parse_option_short(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, _OptStr, []) ->
  175. parse(OptSpecList, OptAcc, ArgAcc, ArgPos, Args).
  176. %% @doc Retrieve the argument for an option from the next string in the list of
  177. %% command-line parameters.
  178. parse_option_next_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, [Arg | Tail] = Args, {Name, _Short, _Long, ArgSpec, _Help} = OptSpec) ->
  179. % Special case for booleans: when the next string is an option we assume
  180. % the value is 'true'.
  181. case (arg_spec_type(ArgSpec) =:= boolean) andalso not is_boolean_arg(Arg) of
  182. true ->
  183. parse(OptSpecList, [{Name, true} | OptAcc], ArgAcc, ArgPos, Args);
  184. _ ->
  185. parse(OptSpecList, [convert_option_arg(OptSpec, Arg) | OptAcc], ArgAcc, ArgPos, Tail)
  186. end;
  187. parse_option_next_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, [] = Args, {Name, _Short, _Long, ArgSpec, _Help}) ->
  188. % Special case for booleans: when the next string is missing we assume the
  189. % value is 'true'.
  190. case arg_spec_type(ArgSpec) of
  191. boolean ->
  192. parse(OptSpecList, [{Name, true} | OptAcc], ArgAcc, ArgPos, Args);
  193. _ ->
  194. throw({error, {missing_option_arg, Name}})
  195. end.
  196. -spec find_non_option_arg([option_spec()], integer()) -> {value, option_spec()} | false.
  197. %% @doc Find the option for the discrete argument in position specified in the
  198. %% Pos argument.
  199. find_non_option_arg([{_Name, undefined, undefined, _ArgSpec, _Help} = OptSpec | _Tail], 0) ->
  200. {value, OptSpec};
  201. find_non_option_arg([{_Name, undefined, undefined, _ArgSpec, _Help} | Tail], Pos) ->
  202. find_non_option_arg(Tail, Pos - 1);
  203. find_non_option_arg([_Head | Tail], Pos) ->
  204. find_non_option_arg(Tail, Pos);
  205. find_non_option_arg([], _Pos) ->
  206. false.
  207. -spec append_default_options([option_spec()], [option()]) -> [option()].
  208. %% @doc Append options that were not present in the command line arguments with
  209. %% their default arguments.
  210. append_default_options([{Name, _Short, _Long, {_Type, DefaultArg}, _Help} | Tail], OptAcc) ->
  211. append_default_options(Tail,
  212. case lists:keymember(Name, 1, OptAcc) of
  213. false ->
  214. [{Name, DefaultArg} | OptAcc];
  215. _ ->
  216. OptAcc
  217. end);
  218. %% For options with no default argument.
  219. append_default_options([_Head | Tail], OptAcc) ->
  220. append_default_options(Tail, OptAcc);
  221. append_default_options([], OptAcc) ->
  222. OptAcc.
  223. -spec convert_option_no_arg(option_spec()) -> option().
  224. convert_option_no_arg({Name, _Short, _Long, ArgSpec, _Help}) ->
  225. case ArgSpec of
  226. % Special case for booleans: if there is no argument we assume
  227. % the value is 'true'.
  228. {boolean, _DefaultValue} ->
  229. {Name, true};
  230. boolean ->
  231. {Name, true};
  232. _ ->
  233. throw({error, {missing_option_arg, Name}})
  234. end.
  235. -spec convert_option_arg(option_spec(), string()) -> option().
  236. %% @doc Convert the argument passed in the command line to the data type
  237. %% indicated by the argument specification.
  238. convert_option_arg({Name, _Short, _Long, ArgSpec, _Help}, Arg) ->
  239. try
  240. {Name, to_type(arg_spec_type(ArgSpec), Arg)}
  241. catch
  242. error:_ ->
  243. throw({error, {invalid_option_arg, {Name, Arg}}})
  244. end.
  245. -spec arg_spec_type(arg_spec()) -> arg_type() | undefined.
  246. %% @doc Retrieve the data type form an argument specification.
  247. arg_spec_type({Type, _DefaultArg}) ->
  248. Type;
  249. arg_spec_type(Type) when is_atom(Type) ->
  250. Type.
  251. -spec to_type(atom(), string()) -> arg_value().
  252. %% @doc Convert an argument string to its corresponding data type.
  253. to_type(binary, Arg) ->
  254. list_to_binary(Arg);
  255. to_type(atom, Arg) ->
  256. list_to_atom(Arg);
  257. to_type(integer, Arg) ->
  258. list_to_integer(Arg);
  259. to_type(float, Arg) ->
  260. list_to_float(Arg);
  261. to_type(boolean, Arg) ->
  262. LowerArg = string:to_lower(Arg),
  263. case is_arg_true(LowerArg) of
  264. true ->
  265. true;
  266. _ ->
  267. case is_arg_false(LowerArg) of
  268. true ->
  269. false;
  270. false ->
  271. erlang:error(badarg)
  272. end
  273. end;
  274. to_type(_Type, Arg) ->
  275. Arg.
  276. -spec is_arg_true(string()) -> boolean().
  277. is_arg_true(Arg) ->
  278. (Arg =:= "true") orelse (Arg =:= "t") orelse
  279. (Arg =:= "yes") orelse (Arg =:= "y") orelse
  280. (Arg =:= "on") orelse (Arg =:= "enabled") orelse
  281. (Arg =:= "1").
  282. -spec is_arg_false(string()) -> boolean().
  283. is_arg_false(Arg) ->
  284. (Arg =:= "false") orelse (Arg =:= "f") orelse
  285. (Arg =:= "no") orelse (Arg =:= "n") orelse
  286. (Arg =:= "off") orelse (Arg =:= "disabled") orelse
  287. (Arg =:= "0").
  288. -spec is_valid_arg(arg_spec() | arg_type(), string()) -> boolean().
  289. is_valid_arg({Type, _DefaultArg}, Arg) ->
  290. is_valid_arg(Type, Arg);
  291. is_valid_arg(boolean, Arg) ->
  292. is_boolean_arg(Arg);
  293. is_valid_arg(integer, Arg) ->
  294. is_integer_arg(Arg);
  295. is_valid_arg(float, Arg) ->
  296. is_float_arg(Arg);
  297. is_valid_arg(_Type, _Arg) ->
  298. true.
  299. -spec is_boolean_arg(string()) -> boolean().
  300. is_boolean_arg(Arg) ->
  301. LowerArg = string:to_lower(Arg),
  302. is_arg_true(LowerArg) orelse is_arg_false(LowerArg).
  303. -spec is_integer_arg(string()) -> boolean().
  304. is_integer_arg([Head | Tail]) when Head >= $0, Head =< $9 ->
  305. is_integer_arg(Tail);
  306. is_integer_arg([_Head | _Tail]) ->
  307. false;
  308. is_integer_arg([]) ->
  309. true.
  310. -spec is_float_arg(string()) -> boolean().
  311. is_float_arg([Head | Tail]) when (Head >= $0 andalso Head =< $9) orelse Head =:= $. ->
  312. is_float_arg(Tail);
  313. is_float_arg([_Head | _Tail]) ->
  314. false;
  315. is_float_arg([]) ->
  316. true.
  317. -spec usage([option_spec()], string()) -> ok.
  318. %%--------------------------------------------------------------------
  319. %% @spec usage(OptSpecList :: [option_spec()], ProgramName :: string()) -> ok.
  320. %% @doc Show a message on stdout indicating the command line options and
  321. %% arguments that are supported by the program.
  322. %%--------------------------------------------------------------------
  323. usage(OptSpecList, ProgramName) ->
  324. io:format("Usage: ~s~s~n~n~s~n",
  325. [ProgramName, usage_cmd_line(OptSpecList), usage_options(OptSpecList)]).
  326. -spec usage([option_spec()], string(), string()) -> ok.
  327. %%--------------------------------------------------------------------
  328. %% @spec usage(OptSpecList :: [option_spec()], ProgramName :: string(), CmdLineTail :: string()) -> ok.
  329. %% @doc Show a message on stdout indicating the command line options and
  330. %% arguments that are supported by the program. The CmdLineTail argument
  331. %% is a string that is added to the end of the usage command line.
  332. %%--------------------------------------------------------------------
  333. usage(OptSpecList, ProgramName, CmdLineTail) ->
  334. io:format("Usage: ~s~s ~s~n~n~s~n",
  335. [ProgramName, usage_cmd_line(OptSpecList), CmdLineTail, usage_options(OptSpecList)]).
  336. -spec usage([option_spec()], string(), string(), [{string(), string()}]) -> ok.
  337. %%--------------------------------------------------------------------
  338. %% @spec usage(OptSpecList :: [option_spec()], ProgramName :: string(),
  339. %% CmdLineTail :: string(), OptionsTail :: [{string(), string()}]) -> ok.
  340. %% @doc Show a message on stdout indicating the command line options and
  341. %% arguments that are supported by the program. The CmdLineTail and OptionsTail
  342. %% arguments are a string that is added to the end of the usage command line
  343. %% and a list of tuples that are added to the end of the options' help lines.
  344. %%--------------------------------------------------------------------
  345. usage(OptSpecList, ProgramName, CmdLineTail, OptionsTail) ->
  346. UsageOptions = lists:foldl(
  347. fun ({Prefix, Help}, Acc) ->
  348. add_option_help(Prefix, Help, Acc)
  349. end, usage_options_reverse(OptSpecList, []), OptionsTail),
  350. io:format("Usage: ~s~s ~s~n~n~s~n",
  351. [ProgramName, usage_cmd_line(OptSpecList), CmdLineTail,
  352. lists:flatten(lists:reverse(UsageOptions))]).
  353. -spec usage_cmd_line([option_spec()]) -> string().
  354. %% @doc Return a string with the syntax for the command line options and
  355. %% arguments.
  356. usage_cmd_line(OptSpecList) ->
  357. usage_cmd_line(OptSpecList, []).
  358. usage_cmd_line([{Name, Short, Long, ArgSpec, _Help} | Tail], Acc) ->
  359. CmdLine =
  360. case ArgSpec of
  361. undefined ->
  362. if
  363. % For options with short form and no argument.
  364. Short =/= undefined ->
  365. [$\s, $[, $-, Short, $]];
  366. % For options with only long form and no argument.
  367. Long =/= undefined ->
  368. [$\s, $[, $-, $-, Long, $]];
  369. true ->
  370. []
  371. end;
  372. _ ->
  373. if
  374. % For options with short form and argument.
  375. Short =/= undefined ->
  376. [$\s, $[, $-, Short, $\s, $<, atom_to_list(Name), $>, $]];
  377. % For options with only long form and argument.
  378. Long =/= undefined ->
  379. [$\s, $[, $-, $-, Long, $\s, $<, atom_to_list(Name), $>, $]];
  380. % For options with neither short nor long form and argument.
  381. true ->
  382. [$\s, $<, atom_to_list(Name), $>]
  383. end
  384. end,
  385. usage_cmd_line(Tail, [CmdLine | Acc]);
  386. usage_cmd_line([], Acc) ->
  387. lists:flatten(lists:reverse(Acc)).
  388. -spec usage_options([option_spec()]) -> string().
  389. %% @doc Return a string with the help message for each of the options and
  390. %% arguments.
  391. usage_options(OptSpecList) ->
  392. lists:flatten(lists:reverse(usage_options_reverse(OptSpecList, []))).
  393. usage_options_reverse([{Name, Short, Long, _ArgSpec, Help} | Tail], Acc) ->
  394. Prefix =
  395. case Long of
  396. undefined ->
  397. case Short of
  398. % Neither short nor long form (non-option argument).
  399. undefined ->
  400. [$<, atom_to_list(Name), $>];
  401. % Only short form.
  402. _ ->
  403. [$-, Short]
  404. end;
  405. _ ->
  406. case Short of
  407. % Only long form.
  408. undefined ->
  409. [$-, $-, Long];
  410. % Both short and long form.
  411. _ ->
  412. [$-, Short, $,, $\s, $-, $-, Long]
  413. end
  414. end,
  415. usage_options_reverse(Tail, add_option_help(Prefix, Help, Acc));
  416. usage_options_reverse([], Acc) ->
  417. Acc.
  418. -spec add_option_help(Prefix :: string(), Help :: string(), Acc :: string()) -> string().
  419. %% @doc Add the help message corresponding to an option specification to a list
  420. %% with the correct indentation.
  421. add_option_help(Prefix, Help, Acc) when is_list(Help), Help =/= [] ->
  422. FlatPrefix = lists:flatten(Prefix),
  423. case ((?INDENTATION * ?TAB_LENGTH) - 2 - length(FlatPrefix)) of
  424. TabSize when TabSize > 0 ->
  425. Tab = lists:duplicate(ceiling(TabSize / ?TAB_LENGTH), $\t),
  426. [[$\s, $\s, FlatPrefix, Tab, Help, $\n] | Acc];
  427. _ ->
  428. % The indentation for the option description is 3 tabs (i.e. 24 characters)
  429. % IMPORTANT: Change the number of tabs below if you change the
  430. % value of the INDENTATION macro.
  431. [[$\t, $\t, $\t, Help, $\n], [$\s, $\s, FlatPrefix, $\n] | Acc]
  432. end;
  433. add_option_help(_Opt, _Prefix, Acc) ->
  434. Acc.
  435. -spec ceiling(float()) -> integer().
  436. %% @doc Return the smallest integral value not less than the argument.
  437. ceiling(X) ->
  438. T = erlang:trunc(X),
  439. case (X - T) of
  440. % Neg when Neg < 0 ->
  441. % T;
  442. Pos when Pos > 0 ->
  443. T + 1;
  444. _ ->
  445. T
  446. end.