Change Svi::Cli to a class

This commit is contained in:
Josh Holtrop 2018-02-27 19:52:28 -05:00
parent d9334aa224
commit 6f55b3ddae
2 changed files with 62 additions and 62 deletions

View File

@ -4,4 +4,6 @@ require "svi"
args = ARGV.dup args = ARGV.dup
exit(Svi::Cli.run(args)) cli = Svi::Cli.new
exit(cli.run(args))

View File

@ -1,76 +1,74 @@
require "yawpa" require "yawpa"
module Svi module Svi
module Cli class Cli
class << self
ALIASES = { ALIASES = {
"co" => "checkout", "co" => "checkout",
} }
HELP_TEXT = <<EOS HELP_TEXT = <<EOS
Usage: svi [options] <command> [parameters...] Usage: svi [options] <command> [parameters...]
Global options: Global options:
--version show svi version and exit --version show svi version and exit
--help, -h show this help and exit --help, -h show this help and exit
Commands: Commands:
checkout/co check out Subversion URL checkout/co check out Subversion URL
EOS EOS
def run(params) def run(params)
options = { options = {
version: {}, version: {},
help: {short: "h"}, help: {short: "h"},
} }
opts, args = Yawpa.parse(params, options, posix_order: true) opts, args = Yawpa.parse(params, options, posix_order: true)
if opts[:version] if opts[:version]
puts "svi, version #{Svi::VERSION}" puts "svi, version #{Svi::VERSION}"
return 0 return 0
end
if opts[:help]
puts HELP_TEXT
return 0
end
if args.size < 1
$stderr.puts HELP_TEXT
return 1
end
run_subcommand(*args)
end end
if opts[:help]
private puts HELP_TEXT
return 0
def run_subcommand(subcommand, *params)
if ALIASES[subcommand]
subcommand = ALIASES[subcommand]
end
command_function = "cmd_#{subcommand}".to_sym
if private_methods.include?(command_function)
Cli.__send__(command_function, params)
else
$stderr.puts "Unknown command #{subcommand}"
1
end
end end
if args.size < 1
def cmd_checkout(params) $stderr.puts HELP_TEXT
options = {} return 1
opts, args = Yawpa.parse(params, options, posix_order: true)
if args.size < 1
$stderr.puts "Error: must specify URL"
return 1
end
url, = args
Svi.checkout(url)
end end
run_subcommand(*args)
def cmd_status(params)
options = {}
opts, args = Yawpa.parse(params, options, posix_order: true)
Svi.status
end
end end
private
def run_subcommand(subcommand, *params)
if ALIASES[subcommand]
subcommand = ALIASES[subcommand]
end
command_function = "cmd_#{subcommand}".to_sym
if private_methods.include?(command_function)
__send__(command_function, params)
else
$stderr.puts "Unknown command #{subcommand}"
1
end
end
def cmd_checkout(params)
options = {}
opts, args = Yawpa.parse(params, options, posix_order: true)
if args.size < 1
$stderr.puts "Error: must specify URL"
return 1
end
url, = args
Svi.checkout(url)
end
def cmd_status(params)
options = {}
opts, args = Yawpa.parse(params, options, posix_order: true)
Svi.status
end
end end
end end