Ruby idiom equivalent to Perl "$asdf = $ARGV[0] || die 'argrequired'"?

G

Giles Bowkett

J

Justin Collins

Giles said:
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
 
M

MonkeeSage

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 Bowkett

Podcast:http://hollywoodgrit.blogspot.com
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. :)

Regards,
Jordan
 
J

Justin Collins

Justin said:
Giles said:
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

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

var = ARGV[0] or raise "Argument required"
 
R

Ryan Davis

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
 
G

Giles Bowkett

I'm making some command-line utilities easier to use and I was
How ruby REALLY stands out... this can be read out loud:

abort "arguments are required" if ARGV.empty?

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 Bowkett

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

Giles Bowkett

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 Bowkett

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

MonkeeSage

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 Bowkett

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
 
T

Tim Hunter

Giles said:
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.
 
J

James Edward Gray II

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 Gray II
 
M

MonkeeSage

$ 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 Gray II

I stand corrected. :)

Regards,
Jordan
 
J

Justin Collins

Giles said:
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.

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
 
G

Giles Bowkett

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.

Yeah, I just wasn't sure what you wanted. Like a more object-oriented
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 Bowkett

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

botp

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top