Last active
December 14, 2015 05:29
-
-
Save riywo/5036022 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
MAIN: { | |
my $cli = CLI->new(); | |
$cli->run(); | |
} | |
package CLI; | |
use Getopt::Long; | |
use Pod::Usage; | |
sub new { | |
my ($class) = @_; | |
my $opts = +{}; | |
GetOptions ( $opts, | |
'help|h', | |
'force|f', | |
) or pod2usage(1); | |
pod2usage(1) if(exists $opts->{help}); | |
bless $opts, $class; | |
} | |
sub run { | |
my ($self) = @_; | |
if (exists $self->{force}) { | |
print "force run\n"; | |
} else { | |
print "run\n"; | |
} | |
} | |
__END__ | |
=head1 SYNOPSIS | |
cli.pl [options] | |
=head1 OPTIONS | |
-f, --force Force run | |
=cut |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from optparse import OptionParser | |
class CLI(object): | |
def __init__(self): | |
parser = OptionParser() | |
parser.add_option("-f", "--force", action="store_true", dest="force", default=False, | |
help="Force run") | |
self.options, self.args = parser.parse_args() | |
def run(self): | |
if self.options.force: | |
print "force run" | |
else: | |
print "run" | |
if __name__ == "__main__": | |
cli = CLI() | |
cli.run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "optparse" | |
class CLI | |
def initialize() | |
@opts = {} | |
ARGV.options do |opt| | |
opt.on("-f", "--force", "Force run") { |v| @opts[:force] = v } | |
opt.parse! | |
end | |
end | |
def run() | |
if @opts.has_key? :force | |
puts "force run" | |
else | |
puts "run" | |
end | |
end | |
end | |
cli = CLI.new | |
cli.run |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./cli.pl -h | |
Usage: | |
cli.pl [options] | |
Options: | |
-f, --force Force run | |
$ ./cli.pl | |
run | |
$ ./cli.pl -f | |
force run | |
################################################ | |
$ ./cli.py -h | |
Usage: cli.py [options] | |
Options: | |
-h, --help show this help message and exit | |
-f, --force Force run | |
$ ./cli.py | |
run | |
$ ./cli.py -f | |
force run | |
################################################ | |
$ ./cli.rb -h | |
Usage: cli [options] | |
-f, --force Force run | |
$ ./cli.rb | |
run | |
$ ./cli.rb -f | |
force run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment