From: "ara.t.howard" Date: 2007-10-20T00:36:01+09:00 Subject: Re: [ANN] main-2.0.0 On Oct 18, 2007, at 10:46 PM, Peņa, Botp wrote: > From: ara.t.howard [mailto:ara.t.howard@gmail.com] > # Subject: [ANN] main-2.0.0 > > quick qs: > > 1. how to invoke -h inside program. > currently i use something like [exec "ruby #{__FILE__} -h"] > > i tried using "usage[]" but i think it's reinventing since -h if > fine for me already. cfp:~/src/ruby/main/main-2.0.0 > cat a.rb require 'main' Main { option 'foobar' def run print usage.to_s exit end } cfp:~/src/ruby/main/main-2.0.0 > ruby a.rb NAME a.rb SYNOPSIS a.rb [options]+ PARAMETERS --foobar --help, -h the reason that the 'to_s' is required is that the usage returns and object that inherits from array and ruby treats 'puts an_array' specially. i should changed that in the next version... regardless 'usage.to_s' will continue to work. > > 2. required args that fail just gives/raise errors which is not so > user friendly. i think 2.0.0 addresses this (common) complaint: cfp:~/src/ruby/main/main-2.0.0 > cat a.rb require 'main' Main { argument('foobar'){ required } run(){ puts Main.version } } cfp:~/src/ruby/main/main-2.0.0 > ruby a.rb foobar 2.0.0 cfp:~/src/ruby/main/main-2.0.0 > ruby a.rb argument(foobar) not given is that ok? > How can i capture error cleanly? so i can display a user friendly > error (and i also want to invoke help thereafter). Maybe require > may need a block for the error to display, like, > hmmmm. right now you, in 2.0.0, you can define 'handle_exception' on your class but you will need to handle all exceptions in that method... that's not great i realize. in 2.0.0 i've added the concept of 'Softspoken' errors. these are errors that, rather than dumping a stacktrace in the log/stderr - simple print their message. that's how the 'argument(foobar) not given' above is printed - it's a 'Softspoken' error and this is it's message. now, as to the question of how to further handle errors but printing usage, etc... that's harder. i'd personally NOT print usage after the error and instead suggest to the user to run the program with '-h' because a long usage message printed after and error will cause the error to scroll off a small terminal... still it's a real need... > required {|error| code_here} > > or > > def err err_obj > end > # ie, main calls err if it exists > # but i still would not know what to put on err > ok i like this idea. the general concept is that parameter can have their own error handlers - let me chew on this a little bit and try to get a sample impl online for you to play with... > Right now, i set required args back to optional and then i have to > ask if args was given? > oh that's hacking we don't want to doing that! ;-) > 3. how can i access all the args and options i set? I'd like to > change my synopsis automatically and beautifully :) > cfp:~/src/ruby/main/main-2.0.0 > cat a.rb require 'main' Main { argument 'foobar' argument 'barfoo' option 'a' option 'b' run(){ params.each{|pm| p pm} } } cfp:~/src/ruby/main/main-2.0.0 > ruby a.rb foobar barfoo --a --b # # # # # i'd be interested to see what you come up with for an auto usage message - that part was very hard for me and i'm not sure mine is the best. > sorry for the many questions, ara. > Thank you for main. It's very cool. glad you are using it, and the feedback is really welcome - it's surprisingly difficult to get this working smoothly for even a modest sample of command line apps - there are a lot of variables! ;-) look for main-2.0.1 later today... cheers. a @ http://codeforpeople.com/ -- it is not enough to be compassionate. you must act. h.h. the 14th dalai lama