[#347506] how do you do this — George George <george.githinji@...>

Given an array of strings e.g.

20 messages 2009/10/01

[#347686] what do you enjoy in a ruby quiz? — Martin DeMello <martindemello@...>

I've noticed that the ruby quiz has been getting few responses of

22 messages 2009/10/04

[#347700] Count the number of times an element occurs in an array — Jim Burgess <jack.zelig@...>

Hi,

13 messages 2009/10/05

[#347715] regex simplifier? — Roger Pack <rogerpack2005@...>

Question.

17 messages 2009/10/05

[#347765] Ruby for the wrong reason — flebber <flebber.crue@...>

Hi

39 messages 2009/10/06
[#347774] Re: Ruby for the wrong reason — 7stud -- <bbxx789_05ss@...> 2009/10/06

flebber wrote:

[#347800] Re: Ruby for the wrong reason — James Edward Gray II <james@...> 2009/10/06

On Oct 6, 2009, at 1:16 AM, 7stud -- wrote:

[#347821] Re: Ruby for the wrong reason — 7stud -- <bbxx789_05ss@...> 2009/10/06

James Edward Gray II wrote:

[#347830] How to not display output of a system call. — Jerry Mr <jerry.piazza@...>

Lets say I have a Windows command line program that runs the following:

11 messages 2009/10/06

[#347871] Google Wave- I need contacts! — Tim Mcd <tmcdowell@...>

After 6 days, i finally got my gWave invite! ('and there was much

21 messages 2009/10/07
[#347889] Re: Google Wave- I need contacts! — Andrew Timberlake <andrew@...> 2009/10/07

Well, you could share the love (I'd love an invite) and build contacts

[#347899] Re: Google Wave- I need contacts! — Tim Mcd <tmcdowell@...> 2009/10/07

Andrew Timberlake wrote:

[#347904] Re: Google Wave- I need contacts! — Rajinder Yadav <devguy.ca@...> 2009/10/07

Tim, when they restrict the rules you could ping this group for an

[#347909] Re: Google Wave- I need contacts! — Tim Mcd <tmcdowell@...> 2009/10/07

Rajinder Yadav wrote:

[#349016] Re: Google Wave- I need contacts! — Jarod Reid <jarod@...> 2009/10/28

i'd like an invite if you have a spare

[#350049] Re: Google Wave- I need contacts! — Rajinder Yadav <devguy.ca@...> 2009/11/10

Jarod Reid wrote:

[#350081] Re: Google Wave- I need contacts! — Michael Weller <gibbsnich@...> 2009/11/10

Uhh, didn't hear of google wave before.

[#348042] Windows Ruby Version Check — James Edward Gray II <james@...>

Can a Windows Ruby user please confirm that the "Ruby 1.9.1-p129

11 messages 2009/10/09

[#348100] Class Level inheritable attributes - are we there yet? — dreamcat four <dreamcat4@...>

Hi,

34 messages 2009/10/10
[#348103] Re: Class Level inheritable attributes - are we there yet? — "David A. Black" <dblack@...> 2009/10/10

Hi --

[#348104] Re: Class Level inheritable attributes - are we there yet? — dreamcat four <dreamcat4@...> 2009/10/10

The problem with class variables in Ruby, is that a class variable is

[#348136] Re: Class Level inheritable attributes - are we there yet? — Robert Klemme <shortcutter@...> 2009/10/11

On 10/10/2009 08:59 PM, dreamcat four wrote:

[#348200] Re: Class Level inheritable attributes - are we there yet? — "ara.t.howard" <ara.t.howard@...> 2009/10/12

[#348231] Re: Class Level inheritable attributes - are we there yet? — "David A. Black" <dblack@...> 2009/10/12

On Tue, 13 Oct 2009, ara.t.howard wrote:

[#348248] Re: Class Level inheritable attributes - are we there yet? — "ara.t.howard" <ara.t.howard@...> 2009/10/13

On Mon, Oct 12, 2009 at 17:48, David A. Black <dblack@rubypal.com> wrote:

[#348258] Re: Class Level inheritable attributes - are we there yet? — Robert Klemme <shortcutter@...> 2009/10/13

2009/10/13 ara.t.howard <ara.t.howard@gmail.com>:

[#348169] Does ruby.h overrides C "enum"? — Iñaki Baz Castillo <ibc@...>

Hi, writting a Ruby C extension (for 1.8 or 1.9) I get an error when using

19 messages 2009/10/12

[#348281] how to solve a special JRuby and Java syntax conflict? — "Axel Etzold" <AEtzold@...>

Dear all,

15 messages 2009/10/13
[#348285] Re: how to solve a special JRuby and Java syntax conflict? — Paul Smith <paul@...> 2009/10/13

On Tue, Oct 13, 2009 at 8:52 PM, Axel Etzold <AEtzold@gmx.de> wrote:

[#348286] Re: how to solve a special JRuby and Java syntax conflict? — Paul Smith <paul@...> 2009/10/13

On Tue, Oct 13, 2009 at 9:52 PM, Paul Smith <paul@pollyandpaul.co.uk> wrote:

[#348287] Re: how to solve a special JRuby and Java syntax conflict? — "Axel Etzold" <AEtzold@...> 2009/10/13

Dear Paul,

[#348290] Re: how to solve a special JRuby and Java syntax conflict? — "Walton Hoops" <walton@...> 2009/10/13

Axel Etzold wrote:

[#348325] Re: how to solve a special JRuby and Java syntax conflict? — "Axel Etzold" <AEtzold@...> 2009/10/14

[#348317] deep cloning, how? — Rajinder Yadav <devguy.ca@...>

I am trying to figure out how to perform a deep clone

29 messages 2009/10/14
[#348330] Re: deep cloning, how? — Robert Klemme <shortcutter@...> 2009/10/14

2009/10/14 Rajinder Yadav <devguy.ca@gmail.com>:

[#348366] Re: deep cloning, how? — Caleb Clausen <vikkous@...> 2009/10/14

On 10/14/09, Robert Klemme <shortcutter@googlemail.com> wrote:

[#348379] Re: deep cloning, how? — Robert Klemme <shortcutter@...> 2009/10/14

On 14.10.2009 19:03, Caleb Clausen wrote:

[#348428] Re: deep cloning, how? — Caleb Clausen <vikkous@...> 2009/10/15

On 10/14/09, Robert Klemme <shortcutter@googlemail.com> wrote:

[#348449] Re: deep cloning, how? — Brian Candler <b.candler@...> 2009/10/16

Caleb Clausen wrote:0

[#348339] Hey you! Stop using relative requires! — Intransition <transfire@...>

I recently came across two different programs that had this line in a

22 messages 2009/10/14
[#348365] Re: Hey you! Stop using relative requires! — Tony Arcieri <tony@...> 2009/10/14

On Wed, Oct 14, 2009 at 6:01 AM, Intransition <transfire@gmail.com> wrote:

[#348401] matching against a zillion patterns — George George <george.githinji@...>

i have some script in which i would like to match a string against

18 messages 2009/10/15

[#348486] Dynamic nested each in ruby 1.8.7? — Toi Toi <toi@...>

How can one have a function that uses a dynamic amount of each

15 messages 2009/10/18

[#348570] memory leak — Rob Doug <broken.m@...>

Hi all,

27 messages 2009/10/20
[#348647] Re: memory leak — Robert Klemme <shortcutter@...> 2009/10/20

On 20.10.2009 03:10, Rob Doug wrote:

[#348655] Re: memory leak — Rob Doug <broken.m@...> 2009/10/20

[#348679] Re: memory leak — Robert Klemme <shortcutter@...> 2009/10/21

On 21.10.2009 00:47, Rob Doug wrote:

[#348718] Re: memory leak — Rob Doug <broken.m@...> 2009/10/22

> You could print out object statistics to get an idea about the source of

[#348727] Creating and raising custom exception in Ruby C extension — Iñaki Baz Castillo <ibc@...>

Hi, I'm trying to create a CustomError exception in a Ruby C extension and raise it:

8 messages 2009/10/22

[#348738] convert/replace a value of nil with 0? — Mmcolli00 Mom <mmc_collins@...>

Do you know how I can convert or replace any value that gets back a

12 messages 2009/10/22

[#348825] mail — Mikel Lindsaar <raasdnil@...>

Some of you might know about a little gem that Minero Aoki wrote called tmail.

15 messages 2009/10/25

[#348877] Array#collect in a method call, not working for me — Michael Randall <randallsata@...>

I am sure I'm making a newbie mistake, as I've just started learning

9 messages 2009/10/26

[#349004] duby 0.0.1 Released — Charles Oliver Nutter <headius@...>

duby version 0.0.1 has been released!

18 messages 2009/10/27
[#349007] Re: [ANN] duby 0.0.1 Released — "Bill Kelly" <billk@...> 2009/10/27

[#349022] Closures / lambda question — Aldric Giacomoni <aldric@...>

This is something I don't understand, and did not understand when I

13 messages 2009/10/28

[#349024] Desktop GUI apps in Ruby — Marnen Laibow-Koser <marnen@...>

Hi folks!

35 messages 2009/10/28

[#349099] Ruby can't subtract ? — Aldric Giacomoni <aldric@...>

I found this blog entry:

25 messages 2009/10/28
[#349100] Re: Ruby can't subtract ? — "Matthew K. Williams" <matt@...> 2009/10/28

On Thu, 29 Oct 2009, Aldric Giacomoni wrote:

[#349183] Using multicore CPUs in parallel tasks — Marc Hoeppner <marc.hoeppner@...>

Hi,

19 messages 2009/10/29
[#349203] Re: Using multicore CPUs in parallel tasks — Glen Holcomb <damnbigman@...> 2009/10/29

On Thu, Oct 29, 2009 at 8:56 AM, Marc Hoeppner

[#349221] Re: Using multicore CPUs in parallel tasks — Tony Arcieri <tony@...> 2009/10/29

On Thu, Oct 29, 2009 at 11:48 AM, Glen Holcomb <damnbigman@gmail.com> wrote:

[#349253] Ruby doesn't implement x++ for Fixnum's because ??? — RichardOnRails <RichardDummyMailbox58407@...>

Hi,

159 messages 2009/10/30
[#349303] Re: Ruby doesn't implement x++ for Fixnum's because ??? — RichardOnRails <RichardDummyMailbox58407@...> 2009/10/30

On Oct 29, 9:18瀾m, RichardOnRails

[#349307] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Robert Klemme <shortcutter@...> 2009/10/30

2009/10/30 RichardOnRails <RichardDummyMailbox58407@uscomputergurus.com>:

[#349337] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Rick DeNatale <rick.denatale@...> 2009/10/30

On Fri, Oct 30, 2009 at 11:05 AM, Robert Klemme

[#349600] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Tony Arcieri <tony@...> 2009/11/04

On Fri, Oct 30, 2009 at 12:03 PM, Rick DeNatale <rick.denatale@gmail.com>wrote:

[#349606] Re: Ruby doesn't implement x++ for Fixnum's because ??? — "Michael W. Ryder" <_mwryder55@...> 2009/11/04

Tony Arcieri wrote:

[#349607] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Tony Arcieri <tony@...> 2009/11/04

On Tue, Nov 3, 2009 at 10:30 PM, Michael W. Ryder <_mwryder55@gmail.com>wrote:

[#349611] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Charles Oliver Nutter <headius@...> 2009/11/04

Of course I had to jump in here.

[#349633] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Rick DeNatale <rick.denatale@...> 2009/11/04

On Wed, Nov 4, 2009 at 1:58 AM, Charles Oliver Nutter

[#349635] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Marnen Laibow-Koser <marnen@...> 2009/11/04

Rick Denatale wrote:

[#349621] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Gavin Sinclair <gsinclair@...> 2009/11/04

On Nov 4, 5:58m, Charles Oliver Nutter <head...@headius.com> wrote:

[#349654] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Tony Arcieri <tony@...> 2009/11/04

On Wed, Nov 4, 2009 at 3:55 AM, Gavin Sinclair <gsinclair@gmail.com> wrote:

[#349665] Re: Ruby doesn't implement x++ for Fixnum's because ??? — "Walton Hoops" <walton@...> 2009/11/04

> -----Original Message-----

[#349671] Re: Ruby doesn't implement x++ for Fixnum's because ??? — "Michael W. Ryder" <_mwryder55@...> 2009/11/04

Walton Hoops wrote:

[#349682] Re: Ruby doesn't implement x++ for Fixnum's because ??? — "Walton Hoops" <walton@...> 2009/11/04

> -----Original Message-----

[#349688] Re: Ruby doesn't implement x++ for Fixnum's because ??? — "Michael W. Ryder" <_mwryder55@...> 2009/11/04

Walton Hoops wrote:

[#349689] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Marnen Laibow-Koser <marnen@...> 2009/11/04

Michael W. Ryder wrote:

[#349693] Re: Ruby doesn't implement x++ for Fixnum's because ??? — "Walton Hoops" <walton@...> 2009/11/04

> -----Original Message-----

[#349814] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Seebs <usenet-nospam@...> 2009/11/06

On 2009-11-06, David A. Black <dblack@rubypal.com> wrote:

[#349879] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Tony Arcieri <tony@...> 2009/11/08

On Fri, Nov 6, 2009 at 4:30 PM, Seebs <usenet-nospam@seebs.net> wrote:

[#349880] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Rick DeNatale <rick.denatale@...> 2009/11/08

On Sun, Nov 8, 2009 at 11:48 AM, Tony Arcieri <tony@medioh.com> wrote:

[#349885] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Tony Arcieri <tony@...> 2009/11/08

On Sun, Nov 8, 2009 at 10:45 AM, Rick DeNatale <rick.denatale@gmail.com>wrote:

[#349886] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Marnen Laibow-Koser <marnen@...> 2009/11/08

Tony Arcieri wrote:

[#349887] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Tony Arcieri <tony@...> 2009/11/08

On Sun, Nov 8, 2009 at 11:22 AM, Marnen Laibow-Koser <marnen@marnen.org>wrote:

[#349889] Re: Ruby doesn't implement x++ for Fixnum's because ??? — "Walton Hoops" <walton@...> 2009/11/08

> -----Original Message-----

[#349892] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Tony Arcieri <tony@...> 2009/11/08

On Sun, Nov 8, 2009 at 11:49 AM, Walton Hoops <walton@vyper.hopto.org>wrote:

[#349898] Re: Ruby doesn't implement x++ for Fixnum's because ??? — "Walton Hoops" <walton@...> 2009/11/08

> From: bascule@gmail.com [mailto:bascule@gmail.com] On Behalf Of Tony

[#349900] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Tony Arcieri <tony@...> 2009/11/08

On Sun, Nov 8, 2009 at 1:15 PM, Walton Hoops <walton@vyper.hopto.org> wrote:

[#349980] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Seebs <usenet-nospam@...> 2009/11/09

On 2009-11-09, Marnen Laibow-Koser <marnen@marnen.org> wrote:

[#349988] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Rick DeNatale <rick.denatale@...> 2009/11/09

On Mon, Nov 9, 2009 at 8:40 AM, Seebs <usenet-nospam@seebs.net> wrote:

[#349989] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Marnen Laibow-Koser <marnen@...> 2009/11/09

Rick Denatale wrote:

[#349994] Re: Ruby doesn't implement x++ for Fixnum's because ??? — Aldric Giacomoni <aldric@...> 2009/11/09

Marnen Laibow-Koser wrote:

[#349264] How do you get the tail end of a string? — "Just Another Victim of the Ambient Morality" <ihatespam@...>

I'm actually hoping this is an embarrassing question but how do you get

55 messages 2009/10/30
[#349266] Re: How do you get the tail end of a string? — "Michael W. Ryder" <_mwryder55@...> 2009/10/30

Just Another Victim of the Ambient Morality wrote:

[#349267] Re: How do you get the tail end of a string? — "Michael W. Ryder" <_mwryder55@...> 2009/10/30

Michael W. Ryder wrote:

[#349286] Re: How do you get the tail end of a string? — Bertram Scharpf <lists@...> 2009/10/30

Hi,

[#349323] Merging hashes using both symbols and strings as keys — shenry <stuarthenry@...>

I'm trying to merge to hashes, one using symbols as keys (the defined

15 messages 2009/10/30
[#349528] Re: Merging hashes using both symbols and strings as keys — Josh Cheek <josh.cheek@...> 2009/11/03

On Fri, Oct 30, 2009 at 11:10 AM, shenry <stuarthenry@gmail.com> wrote:

[#349352] In-place parameter modification — Dave Anderson <anderson@...>

Native to ruby are several methods that change passed-in parameters

17 messages 2009/10/30

[#349406] Hamurabi (#223) — Daniel Moore <yahivin@...>

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

23 messages 2009/10/31
[#349451] Re: [QUIZ] Hamurabi (#223) — James Edward Gray II <james@...> 2009/11/01

On Oct 31, 2009, at 6:23 PM, Daniel Moore wrote:

[#349498] Re: [QUIZ] Hamurabi (#223) — Martin DeMello <martindemello@...> 2009/11/02

On Mon, Nov 2, 2009 at 5:29 AM, James Edward Gray II

[ANN] main-4.0.0 (for avdi)

From: "ara.t.howard" <ara.t.howard@...>
Date: 2009-10-21 00:21:41 UTC
List: ruby-talk #348663
NAME
  main.rb

SYNOPSIS
  a class factory and dsl for generating command line programs real quick

URI
  http://codeforpeople.com/lib/ruby/
  http://rubyforge.org/projects/codeforpeople/
  http://github.com/ahoward/main

INSTALL
  gem install main

DESCRIPTION
  main.rb features the following:

    - unification of option, argument, keyword, and environment parameter
      parsing
    - auto generation of usage and help messages
    - support for mode/sub-commands
    - io redirection support
    - logging hooks using ruby's built-in logging mechanism
    - intelligent error handling and exit codes
    - use as dsl or library for building Main objects
    - parsing user defined ARGV and ENV
    - zero requirements for understanding the obtuse apis of *any* command
      line option parsers
    - leather pants

  in short main.rb aims to drastically lower the barrier to writing uniform
  command line applications.

  for instance, this program

    require 'main'

    Main {
      argument 'foo'
      option 'bar'

      def run
        p params['foo']
        p params['bar']
        exit_success!
      end
    }

  sets up a program which requires one argument, 'bar', and which may accept one
  command line switch, '--foo' in addition to the single option/mode
which is always
  accepted and handled appropriately: 'help', '--help', '-h'.  for the most
  part main.rb stays out of your command line namespace but insists that your
  application has at least a help mode/option.

  main.rb supports sub-commands in a very simple way

    require 'main'

    Main {
      mode 'install' do
        def run() puts 'installing...' end
      end

      mode 'uninstall' do
        def run() puts 'uninstalling...' end
      end
    }

  which allows a program, called 'a.rb', to be invoked as

    ruby a.rb install

  and

    ruby a.rb uninstall

  for simple programs main.rb is a real time saver but it's for more complex
  applications where main.rb's unification of parameter parsing, class
  configuration dsl, and auto-generation of usage messages can really streamline
  command line application development.  for example the following 'a.rb'
  program:

    require 'main'

    Main {
      argument('foo'){
        cast :int
      }
      keyword('bar'){
        arity 2
        cast :float
        defaults 0.0, 1.0
      }
      option('foobar'){
        argument :optional
        description 'the foobar option is very handy'
      }
      environment('BARFOO'){
        cast :list_of_bool
        synopsis 'export barfoo=value'
      }

      def run
        p params['foo'].value
        p params['bar'].values
        p params['foobar'].value
        p params['BARFOO'].value
      end
    }

  when run with a command line of

    BARFOO=true,false,false ruby a.rb 42 bar=40 bar=2 --foobar=a

  will produce

    42
    [40.0, 2.0]
    "a"
    [true, false, false]

  while a command line of

    ruby a.rb --help

  will produce

    NAME
      a.rb

    SYNOPSIS
      a.rb foo [bar=bar] [options]+

    PARAMETERS
      * foo [ 1 -> int(foo) ]

      * bar=bar [ 2 ~> float(bar=0.0,1.0) ]

      * --foobar=[foobar] [ 1 ~> foobar ]
          the foobar option is very handy

      * --help, -h

      * export barfoo=value

  and this shows how all of argument, keyword, option, and environment parsing
  can be declartively dealt with in a unified fashion - the dsl for all
  parameter types is the same - and how auto synopsis and usage generation saves
  keystrokes.  the parameter synopsis is compact and can be read as

      * foo [ 1 -> int(foo) ]

        'one argument will get processed via int(argument_name)'

          1        : one argument
          ->       : will get processed (the argument is required)
          int(foo) : the cast is int, the arg name is foo

      * bar=bar [ 2 ~> float(bar=0.0,1.0) ]

        'two keyword arguments might be processed via float(bar=0.0,1.0)'

          2                  : two arguments
          ~>                 : might be processed (the argument is optional)
          float(bar=0.0,1.0) : the cast will be float, the default values are
                               0.0 and 1.0

      * --foobar=[foobar] [ 1 ~> foobar ]

        'one option with optional argument may be given directly'

      * --help, -h

        no synopsis, simple switch takes no args and is not required

      * export barfoo=value

        a user defined synopsis

SAMPLES

  <========< samples/a.rb >========>

  ~ > cat samples/a.rb

    require 'main'

    ARGV.replace %w( 42 ) if ARGV.empty?

    Main {
      argument('foo'){
        required                    # this is the default
        cast :int                   # value cast to Fixnum
        validate{|foo| foo == 42}   # raises error in failure case
        description 'the foo param' # shown in --help
      }

      def run
        p params['foo'].given?
        p params['foo'].value
      end
    }

  ~ > ruby samples/a.rb

    true
    42


  <========< samples/b.rb >========>

  ~ > cat samples/b.rb

    require 'main'

    ARGV.replace %w( 40 1 1 ) if ARGV.empty?

    Main {
      argument('foo'){
        arity 3                             # foo will given three times
        cast :int                           # value cast to Fixnum
        validate{|foo| [40,1].include? foo} # raises error in failure case
        description 'the foo param'         # shown in --help
      }

      def run
        p params['foo'].given?
        p params['foo'].values
      end
    }

  ~ > ruby samples/b.rb

    true
    [40, 1, 1]


  <========< samples/c.rb >========>

  ~ > cat samples/c.rb

    require 'main'

    ARGV.replace %w( foo=40 foo=2 bar=false ) if ARGV.empty?

    Main {
      keyword('foo'){
        required  # by default keywords are not required
        arity 2
        cast :float
      }
      keyword('bar'){
        cast :bool
      }

      def run
        p params['foo'].given?
        p params['foo'].values
        p params['bar'].given?
        p params['bar'].value
      end
    }

  ~ > ruby samples/c.rb

    true
    [40.0, 2.0]
    true
    false


  <========< samples/d.rb >========>

  ~ > cat samples/d.rb

    require 'main'

    ARGV.replace %w( --foo=40 -f2 ) if ARGV.empty?

    Main {
      option('foo', 'f'){
        required  # by default options are not required, we could use 'foo=foo'
                  # above as a shortcut
        argument_required
        arity 2
        cast :float
      }

      option('bar=[bar]', 'b'){  # note shortcut syntax for optional args
        # argument_optional      # we could also use this method
        cast :bool
        default false
      }

      def run
        p params['foo'].given?
        p params['foo'].values
        p params['bar'].given?
        p params['bar'].value
      end
    }

  ~ > ruby samples/d.rb

    true
    [40.0, 2.0]
    nil
    false


  <========< samples/e.rb >========>

  ~ > cat samples/e.rb

    require 'main'

    ARGV.replace %w( x y argument )

    Main {
      argument 'argument'
      option 'option'

      def run() puts 'run' end

      mode 'a' do
        option 'a-option'
        def run() puts 'a-run' end
      end

      mode 'x' do
        option 'x-option'

        def run() puts 'x-run' end

          mode 'y' do
            option 'y-option'

            def run() puts 'y-run' end
          end
      end
    }

  ~ > ruby samples/e.rb

    y-run


  <========< samples/f.rb >========>

  ~ > cat samples/f.rb

    require 'main'

    ARGV.replace %W( compress /data )

    Main {
      argument('directory'){ description 'the directory to operate on' }

      option('force'){ description 'use a bigger hammer' }

      def run
        puts 'this is how we run when no mode is specified'
      end

      mode 'compress' do
        option('bzip'){ description 'use bzip compression' }

        def run
          puts 'this is how we run in compress mode'
        end
      end

      mode 'uncompress' do
        option('delete-after'){ description 'delete orginal file after
uncompressing' }

        def run
          puts 'this is how we run in un-compress mode'
        end
      end
    }

  ~ > ruby samples/f.rb

    this is how we run in compress mode


  <========< samples/g.rb >========>

  ~ > cat samples/g.rb

    require 'main'

    ARGV.replace %w( 42 ) if ARGV.empty?

    Main {
      argument( 'foo' )
      option( 'bar' )

      run { puts "This is what to_options produces:
#{params.to_options.inspect}" }
    }

  ~ > ruby samples/g.rb

    This is what to_options produces: {"help"=>nil, "foo"=>"42", "bar"=>nil}


  <========< samples/h.rb >========>

  ~ > cat samples/h.rb

    require 'main'

    # block-defaults are instance_eval'd in the main instance and be
combined with
    # mixins
    #
    # ./h.rb   #=> forty-two
    # ./h.rb a #=> 42
    # ./h.rb b #=> 42.0
    #

    Main {
      fattr :default_for_foobar => 'forty-two'

      option(:foobar) do
        default{ default_for_foobar }
      end

      mixin :foo do
        fattr :default_for_foobar => 42
      end

      mixin :bar do
        fattr :default_for_foobar => 42.0
      end


      run{ p params[:foobar].value }

      mode :a do
        mixin :foo
      end

      mode :b do
        mixin :bar
      end
    }

  ~ > ruby samples/h.rb

    "forty-two"



DOCS
  test/main.rb
  vim -p lib/main.rb lib/main/*rb
  API section below

HISTORY
  4.0.0
    - avoid duping ios.  new methods Main.push_ios! and Main.pop_ios! are
    utilized for testing.  this was done to make it simple to wrap
    daemon/servolux programs around main, althought not strictly required.
    not the version bump - there is not reason to expect existing main
    programs to break, but it *is* and interface change which requires a major
    version bump.


API

  Main {

  ###########################################################################
  #                       CLASS LEVEL API                                   #
  ###########################################################################
  #
  # the name of the program, auto-set and used in usage
  #
    program 'foo.rb'
  #
  # a short description of program functionality, auto-set and used in usage
  #
    synopsis "foo.rb arg [options]+"
  #
  # long description of program functionality, used in usage iff set
  #
    description <<-hdoc
      this text will automatically be indented to the right level.

      it should describe how the program works in detail
    hdoc
  #
  # used in usage iff set
  #
    author 'ara.t.howard@gmail.com'
  #
  # used in usage
  #
    version '0.0.42'
  #
  # stdin/out/err can be anthing which responds to read/write or a string
  # which will be opened as in the appropriate mode
  #
    stdin '/dev/null'
    stdout '/dev/null'
    stderr open('/dev/null', 'w')
  #
  # the logger should be a Logger object, something 'write'-able, or a string
  # which will be used to open the logger.  the logger_level specifies the
  # initalize verbosity setting, the default is Logger::INFO
  #
    logger(( program + '.log' ))
    logger_level Logger::DEBUG
  #
  # you can configure exit codes.  the defaults are shown
  #
    exit_success # 0
    exit_failure # 1
    exit_warn    # 42
  #
  # the usage object is rather complex.  by default it's an object which can
  # be built up in sections using the
  #
  #   usage["BUGS"] = "something about bugs'
  #
  # syntax to append sections onto the already pre-built usage message which
  # contains program, synopsis, parameter descriptions and the like
  #
  # however, you always replace the usage object wholesale with one of your
  # chosing like so
  #
    usage <<-txt
      my own usage message
    txt

  ###########################################################################
  #                         MODE API                                        #
  ###########################################################################
  #
  # modes are class factories that inherit from their parent class.  they can
  # be nested *arbitrarily* deep.  usage messages are tailored for each mode.
  # modes are, for the most part, independant classes but parameters are
  # always a superset of the parent class - a mode accepts all of it's parents
  # paramters *plus* and additional ones
  #
    option 'inherited-option'
    argument 'inherited-argument'

    mode 'install' do
      option 'force' do
        description 'clobber existing installation'
      end

      def run
        inherited_method()
        puts 'installing...'
      end

      mode 'docs' do
        description 'installs the docs'

        def run
          puts 'installing docs...'
        end
      end
    end

    mode 'un-install' do
      option 'force' do
        description 'remove even if dependancies exist'
      end

      def run
        inherited_method()
        puts 'un-installing...'
      end
    end

    def run
      puts 'no mode yo?'
    end

    def inherited_method
      puts 'superclass_method...'
    end


  ###########################################################################
  #                         PARAMETER API                                   #
  ###########################################################################
  #
  # all the parameter types of argument|keyword|option|environment share this
  # api.  you must specify the type when the parameter method is used.
  # alternatively used one of the shortcut methods
  # argument|keyword|option|environment.  in otherwords
  #
  #   parameter('foo'){ type :option }
  #
  # is synonymous with
  #
  #   option('foo'){ }
  #
    option 'foo' {
    #
    # required - whether this paramter must by supplied on the command line.
    # note that you can create 'required' options with this keyword
    #
      required # or required true
    #
    # argument_required - applies only to options.
    #
      argument_required # argument :required
    #
    # argument_optional - applies only to options.
    #
      argument_optional # argument :optional
    #
    # cast - should be either a lambda taking one argument, or a symbol
    # designation one of the built in casts defined in Main::Cast.  supported
    # types are :boolean|:integer|:float|:numeric|:string|:uri.  built-in
    # casts can be abbreviated
    #
      cast :int
    #
    # validate - should be a lambda taking one argument and returning
    # true|false
    #
      validate{|int| int == 42}
    #
    # synopsis - should be a concise characterization of the paramter.  a
    # default synopsis is built automatically from the parameter.  this
    # information is displayed in the usage message
    #
      synopsis '--foo'
    #
    # description - a longer description of the paramter.  it appears in the
    # usage also.
    #
      description 'a long description of foo'
    #
    # arity - indicates how many times the parameter should appear on the
    # command line.  the default is one.  negative arities are supported and
    # follow the same rules as ruby methods/procs.
    #
      arity 2
    #
    # default - you can provide a default value in case none is given.  the
    # alias 'defaults' reads a bit nicer when you are giving a list of
    # defaults for paramters of > 1 arity
    #
      defaults 40, 2
    #
    # you can add custom per-parameter error handlers using the following
    #
      error :before do
        puts 'this fires *before* normal error handling using #instance_eval...'
      end

      error do
        puts 'this fires *instead of* normal error handling using
#instance_eval...'
      end

      error :after do
        puts 'this fires *after* normal error handling using #instance_eval...'
      end
    }

  ###########################################################################
  #                       INSTANCE LEVEL API                                #
  ###########################################################################
  #
  # you must define a run method.  it is the only method you must define.
  #
    def run
      #
      # all parameters are available in the 'params' hash and via the alias
      # 'param'.  it can be indexed via string or symbol.  the values are all
      # Main::Parameter objects
      #
        foo = params['foo']
      #
      # the given? method indicates whether or not the parameter was given on
      # the commandline/environment, etc.  in particular this will not be true
      # when a default value was specified but no parameter was given
      #
        foo.given?
      #
      # the list of all values can be retrieved via 'values'.  note that this
      # is always an array.
      #
        p foo.values
      #
      # the __first__ value can be retrieved via 'value'.  note that this
      # never an array.
      #
        p foo.value
      #
      # the methods debug|info|warn|error|fatal are delegated to the logger
      # object
      #
        info{ "this goes to the log" }
      #
      # you can set the exit_status at anytime.  this status is used when
      # exiting the program.  exceptions cause this to be ext_failure if, and
      # only if, the current value was exit_success.  in otherwords an
      # un-caught exception always results in a failing exit_status
      #
        exit_status exit_failure
      #
      # a few shortcuts both set the exit_status and exit the program.
      #
        exit_success!
        exit_failure!
        exit_warn!
    end

  }


-- 
-a
--
be kind whenever possible... it is always possible - h.h. the 14th dalai lama

In This Thread

Prev Next