From 4c715eaa94f8f94d5a4e73628e8b6e32a5c10c2b Mon Sep 17 00:00:00 2001 From: Juan Jose Comellas Date: Tue, 13 Oct 2009 18:35:23 -0300 Subject: [PATCH] Added example file and make target. --- Emakefile | 1 + Makefile | 9 +++-- src/examples/ex1.erl | 78 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 src/examples/ex1.erl diff --git a/Emakefile b/Emakefile index c3f170f..e79e2aa 100644 --- a/Emakefile +++ b/Emakefile @@ -1,2 +1,3 @@ {"src/*", [debug_info, {outdir, "ebin"}, {i, "include"}]}. {"src/test/*", [debug_info, {outdir, "ebin"}, {i, "include"}]}. +{"src/examples/*", [debug_info, {outdir, "ebin"}, {i, "include"}]}. diff --git a/Makefile b/Makefile index 8dabb02..73b92f6 100644 --- a/Makefile +++ b/Makefile @@ -7,16 +7,19 @@ DOC_OPTS={dir, \"doc\"}, {includes, [\"include\"]}, {source_path, [\"include\", all: @mkdir -p ebin - $(ERL) $(EPATH) -make + @$(ERL) $(EPATH) -make run: all $(ERL) -sname "$(PROJECT)" $(EPATH) test: all - $(ERL) -noshell $(EPATH) -s $(PROJECT)_test test -s init stop + @$(ERL) -noshell $(EPATH) -s $(PROJECT)_test test -s init stop + +examples: all + @$(ERL) -noshell $(EPATH) -s ex1 test -s init stop doc: all - $(ERL) -noshell $(EPATH) \ + @$(ERL) -noshell $(EPATH) \ -eval "edoc:files(filelib:wildcard(\"$(SOURCES)\"), [$(DOC_OPTS)])" \ -s init stop diff --git a/src/examples/ex1.erl b/src/examples/ex1.erl new file mode 100644 index 0000000..c6c9a43 --- /dev/null +++ b/src/examples/ex1.erl @@ -0,0 +1,78 @@ +%%%------------------------------------------------------------------- +%%% @author Juan Jose Comellas +%%% @copyright (C) 2009, Novamens SA (http://www.novamens.com) +%%% @doc Example module for Parses command line options with a format similar to that of GNU getopt. +%%% @end +%%%------------------------------------------------------------------- +-module(ex1). + +-include("include/getopt.hrl"). + +-export([test/0, test/1]). + +test() -> + test("-U myuser -P mypassword --host myhost -x -o myfile.dump mydb dummy1"). + + +test(CmdLine) -> + OptSpecList = option_spec(), + + io:format("For command line: ~p~n" + "getopt:parse/2 returns:~n~n", [CmdLine]), + case getopt:parse(OptSpecList, CmdLine) of + {ok, {Options, NonOptArgs}} -> + io:format("Options:~n ~p~nNon-option arguments:~n ~p~n", [Options, NonOptArgs]); + {error, {Reason, Data}} -> + io:format("Error: ~s ~p~n~n", [Reason, Data]), + getopt:usage(OptSpecList, "ex1") + end. + + +option_spec() -> + CurrentUser = os:getenv("USER"), + [ + #option{name = help, + short = $?, + long = "help", + help = "Show the program options" + }, + #option{name = username, + short = $U, + long = "username", + arg = string, + help = "Username to connect to the database" + }, + #option{name = password, + short = $P, + long = "password", + arg = {string, CurrentUser}, + help = "Password to connect to the database" + }, + #option{name = host, + short = $h, + long = "host", + arg = {string, "localhost"}, + help = "Database server host name or IP address" + }, + #option{name = port, + short = $p, + long = "port", + arg = {integer, 1000}, + help = "Database server port" + }, + #option{name = output_file, + short = $o, + long = "output-file", + arg = string, + help = "File where the data will be saved to" + }, + #option{name = xml, + short = $x, + long = "xml", + help = "Output data as XML" + }, + #option{name = dbname, + arg = string, + help = "Database name" + } + ].