Ruby idiom equivalent to Perl "$asdf = $ARGV|| die 'arg required'"?

I’m making some command-line utilities easier to use and I was
wondering if there’s a really nice simple Rubyish way to do what I
used to do with Perl back in the day. All my code in these utils which
exit and print usage strings in the absence of required command-line
args is looking kinda clunky to me.


Giles B.

Podcast: http://hollywoodgrit.blogspot.com
Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com

Giles B. wrote:

I’m making some command-line utilities easier to use and I was
wondering if there’s a really nice simple Rubyish way to do what I
used to do with Perl back in the day. All my code in these utils which
exit and print usage strings in the absence of required command-line
args is looking kinda clunky to me.

Are you saying you do want something like the example you gave?

var = ARGV[0] || raise(“Argument required”)

-Justin

On Dec 4, 12:41 am, Giles B. [email protected] wrote:

Blog:http://gilesbowkett.blogspot.com
Portfolio:http://www.gilesgoatboy.org
Tumblelog:http://giles.tumblr.com

You can do something like this, but it’s ugly (parens necessary)…

asdf = ARGV[0] || (puts(“argument required”); exit 1)

Better…

def die(message)
puts message
exit 1
end

asdf = ARGV[0] || die(“argument required”)

But I would still probably write something like this instead…

class Array
def at!(index, message)
if self.at(index)
self.at(index)
else
puts message
exit 1
end
end
end

asdf = ARGV.at!(0, “argument required”)

…though I guess that’s really a matter of preference. :slight_smile:

Regards,
Jordan

On Dec 3, 2007, at 22:41 , Giles B. wrote:

I’m making some command-line utilities easier to use and I was
wondering if there’s a really nice simple Rubyish way to do what I
used to do with Perl back in the day. All my code in these utils which
exit and print usage strings in the absence of required command-line
args is looking kinda clunky to me.

How ruby REALLY stands out… this can be read out loud:

abort “arguments are required” if ARGV.empty?

ARGV.each do |arg|

end

Justin C. wrote:

var = ARGV[0] || raise(“Argument required”)

-Justin

I guess you can get rid of the parentheses, too.

var = ARGV[0] or raise “Argument required”

ARGV.each do |arg|

end

abort!

cool. that’s better than die. I remember one time talking with a
fellow Perl guy, figuring out what a program was doing, and I realized
we had used the word “kill” like thirty times in a minute of
discussion. “abort” sounds much better. like Star Trek.


Giles B.

Podcast: http://hollywoodgrit.blogspot.com
Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com

Are you saying you do want something like the example you gave?

var = ARGV[0] || raise(“Argument required”)

Yeah, because I’m not just a Rails programmer, so I know other
languages have merit too.

Wait, sorry, that was lame of me.


Giles B.

Podcast: http://hollywoodgrit.blogspot.com
Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com

Giles B. wrote:

cool. that’s better than die. I remember one time talking with a
fellow Perl guy, figuring out what a program was doing, and I realized
we had used the word “kill” like thirty times in a minute of
discussion. “abort” sounds much better. like Star Trek.

“abort” and “kill” are way too negative and violent. I use
“succeed_at_alternate_goal” instead.

I’m making some command-line utilities easier to use and I was
wondering if there’s a really nice simple Rubyish way to do what I
used to do with Perl back in the day. All my code in these utils which
exit and print usage strings in the absence of required command-line
args is looking kinda clunky to me.

Are you saying you do want something like the example you gave?

var = ARGV[0] || raise(“Argument required”)

Yeah, because I’m not just a Rails programmer, so I know other
languages have merit too.


Giles B.

Podcast: http://hollywoodgrit.blogspot.com
Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com

On Dec 4, 2007, at 11:08 AM, MonkeeSage wrote:

raise/fail don’t have perl compatibility…they print nasty stuff
about errors and what-not. In perl, “die” just prints a message and
dies (how suprising). If you really want something equiv. to the perl,
you’ll have to write your own #die or do something more extensive.

$ qri Kernel#abort
----------------------------------------------------------- Kernel#abort
abort
Kernel::abort
Process::abort

  Terminate execution immediately, effectively by calling
  Kernel.exit(1). If msg is given, it is written to STDERR prior to
  terminating.

James Edward G. II

On Dec 4, 11:30 am, James Edward G. II [email protected]
wrote:

  Kernel::abort
  Process::abort

  Terminate execution immediately, effectively by calling
  Kernel.exit(1). If msg is given, it is written to STDERR prior to
  terminating.

James Edward G. II

I stand corrected. :slight_smile:

Regards,
Jordan

“abort” and “kill” are way too negative and violent.

How about

stop

:wink:

On Dec 4, 10:31 am, Giles B. [email protected] wrote:

Yeah, because I’m not just a Rails programmer, so I know other
languages have merit too.


Giles B.

Podcast:http://hollywoodgrit.blogspot.com
Blog:http://gilesbowkett.blogspot.com
Portfolio:http://www.gilesgoatboy.org
Tumblelog:http://giles.tumblr.com

raise/fail don’t have perl compatibility…they print nasty stuff
about errors and what-not. In perl, “die” just prints a message and
dies (how suprising). If you really want something equiv. to the perl,
you’ll have to write your own #die or do something more extensive.

Regards,
Jordan

Giles B. wrote:

Yeah, I just wasn’t sure what you wanted. Like a more object-oriented
version or what.

Looks like abort wins the day, though.

var = ARGV[0] or abort “Arg required”

-Justin

version or what.
My bad.

Looks like abort wins the day, though.

var = ARGV[0] or abort “Arg required”

or Ryan’s version:

abort “arguments are required” if ARGV.empty?

Although actually, I’m using optparse, and it’s sometimes useful to do

usage_error += “need foo” unless @foo
usage_error += “need bar” unless @bar
abort usage_error unless usage_error.empty?


Giles B.

Podcast: http://hollywoodgrit.blogspot.com
Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com

On Dec 5, 2007 6:01 AM, Giles B. [email protected] wrote:

Although actually, I’m using optparse, and it’s sometimes useful to do
usage_error += “need foo” unless @foo
usage_error += “need bar” unless @bar
abort usage_error unless usage_error.empty?

Ah, may i suggest Ara’s main gem …

C:\family\ruby>cat test.rb
require ‘rubygems’
require ‘main’

Main {
argument ‘foo’
argument ‘bar’

def run
p params[‘foo’]
p params[‘bar’]
end
}

C:\family\ruby>ruby test.rb
argument(foo) not given

C:\family\ruby>ruby test.rb foo
argument(bar) not given

C:\family\ruby>ruby test.rb foo bar
#<Main::Parameter::Argument:0x2b59bfc @cast=nil, @arity=1,
@values=[“foo”], @names=[“foo”], @defaults=[], @required=true,
@validate=nil, @given=true, @type=:argument>
#<Main::Parameter::Argument:0x2b598dc @cast=nil, @arity=1,
@values=[“bar”], @names=[“bar”], @defaults=[], @required=true,
@validate=nil, @given=true, @type=:argument>

C:\family\ruby>ruby test.rb -h
NAME
test.rb

SYNOPSIS
test.rb foo bar [options]+

PARAMETERS
foo (1 -> foo)
bar (1 -> bar)
–help, -h

kind regards -botp

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs