From c181cfc4e6169de0d50c303d15d00545bf15d9cf Mon Sep 17 00:00:00 2001 From: Simon Skorokhodov <skorohodovsemen@gmail.com> Date: Tue, 6 Mar 2018 20:53:24 +0100 Subject: [PATCH] Add `utf8_binary` type --- src/getopt.erl | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/getopt.erl b/src/getopt.erl index b5ee7b4..847a98a 100644 --- a/src/getopt.erl +++ b/src/getopt.erl @@ -13,7 +13,7 @@ -export([parse/2, check/2, parse_and_check/2, format_error/2, usage/2, usage/3, usage/4, usage/6, tokenize/1]). --export([usage_cmd_line/2]). +-export([usage_cmd_line/2, usage_options/1]). -define(LINE_LENGTH, 75). -define(MIN_USAGE_COMMAND_LINE_OPTION_LENGTH, 25). @@ -30,7 +30,7 @@ (Char) =:= $\n orelse (Char) =:= $\r)). %% Atom indicating the data type that an argument can be converted to. --type arg_type() :: 'atom' | 'binary' | 'boolean' | 'float' | 'integer' | 'string'. +-type arg_type() :: 'atom' | 'binary' | 'utf8_binary' | 'boolean' | 'float' | 'integer' | 'string'. %% Data type that an argument can be converted to. -type arg_value() :: atom() | binary() | boolean() | float() | integer() | string(). %% Argument specification. @@ -435,6 +435,8 @@ to_type({Type, _DefaultArg}, Arg) -> to_type(Type, Arg); to_type(binary, Arg) -> list_to_binary(Arg); +to_type(utf8_binary, Arg) -> + unicode:characters_to_nfc_binary(Arg); to_type(atom, Arg) -> list_to_atom(Arg); to_type(integer, Arg) -> @@ -730,8 +732,8 @@ usage_option_text({_Name, Short, Long, _ArgSpec, _Help}) -> -spec usage_help_text(option_spec()) -> string(). -usage_help_text({_Name, _Short, _Long, {_ArgType, ArgValue}, [_ | _] = Help}) -> - Help ++ " [default: " ++ default_arg_value_to_string(ArgValue) ++ "]"; +usage_help_text({_Name, _Short, _Long, {ArgType, ArgValue}, [_ | _] = Help}) -> + Help ++ " [default: " ++ default_arg_value_to_string(ArgType, ArgValue) ++ "]"; usage_help_text({_Name, _Short, _Long, _ArgSpec, Help}) -> Help. @@ -804,15 +806,17 @@ wrap_text_line(_Length, [], Acc, _Count, _CurrentLineAcc) -> lists:reverse(Acc). -default_arg_value_to_string(Value) when is_atom(Value) -> +default_arg_value_to_string(_, Value) when is_atom(Value) -> atom_to_list(Value); -default_arg_value_to_string(Value) when is_binary(Value) -> +default_arg_value_to_string(binary, Value) when is_binary(Value) -> binary_to_list(Value); -default_arg_value_to_string(Value) when is_integer(Value) -> +default_arg_value_to_string(utf8_binary, Value) when is_binary(Value) -> + unicode:characters_to_list(Value); +default_arg_value_to_string(_, Value) when is_integer(Value) -> integer_to_list(Value); -default_arg_value_to_string(Value) when is_float(Value) -> +default_arg_value_to_string(_, Value) when is_float(Value) -> lists:flatten(io_lib:format("~w", [Value])); -default_arg_value_to_string(Value) -> +default_arg_value_to_string(_, Value) -> Value. -- 2.19.0