Codebase list erlang-getopt / eb4a5c8
Add example with very long help text to show wrapped usage output Juan Jose Comellas 11 years ago
1 changed file(s) with 56 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 #!/usr/bin/env escript
1 %% -*- erlang -*-
2 %%! -sname ex1 -pz ebin
3
4 %%%-------------------------------------------------------------------
5 %%% @author Juan Jose Comellas <juanjo@comellas.org>
6 %%% @copyright (C) 2009 Juan Jose Comellas
7 %%% @doc Example file for the getopt module.
8 %%% @end
9 %%%
10 %%% This source file is subject to the New BSD License. You should have received
11 %%% a copy of the New BSD license with this software. If not, it can be
12 %%% retrieved from: http://www.opensource.org/licenses/bsd-license.php
13 %%%-------------------------------------------------------------------
14 -module(ex1).
15 -author('juanjo@comellas.org').
16
17 main([]) ->
18 getopt:usage(option_spec_list(), escript:script_name());
19 main(Args) ->
20 OptSpecList = option_spec_list(),
21 io:format("For command line: ~p~n"
22 "getopt:parse/2 returns:~n~n", [Args]),
23 case getopt:parse(OptSpecList, Args) of
24 {ok, {Options, NonOptArgs}} ->
25 io:format("Options:~n ~p~n~nNon-option arguments:~n ~p~n", [Options, NonOptArgs]);
26 {error, {Reason, Data}} ->
27 io:format("Error: ~s ~p~n~n", [Reason, Data]),
28 getopt:usage(OptSpecList, "ex1.escript")
29 end.
30
31
32 option_spec_list() ->
33 CurrentUser = case os:getenv("USER") of
34 false ->
35 "user";
36 User ->
37 User
38 end,
39 LongHelp = "Password to connect to the database; this is an arbitrarily long string just to check "
40 "whether getopt wraps lines correctly when they become too long for the console. We'll "
41 "make it extra long so that it takes at least 3 lines to show on normal consoles. That "
42 "wasn't enough, so let's try again by adding more and more redundant text to this help "
43 "line. What if we copied a paragraph from a book? That would surely make it easier",
44 [
45 %% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg}
46 {help, $?, "help", undefined, "Show the program options"},
47 {username, $U, "username", {string, CurrentUser}, "Username to connect to the database"},
48 {password, $P, "password", string, LongHelp},
49 {host, $h, "host", {string, "localhost"}, "Database server host name or IP address"},
50 {port, $p, "port", {integer, 1000}, "Database server port"},
51 {output_file, $o, "output-file", string, "File where the data will be saved to"},
52 {xml, $x, "xml", undefined, "Output data as XML"},
53 {verbose, $v, "verbose", integer, "Verbosity level"},
54 {dbname, undefined, undefined, string, "Database name"}
55 ].