英文:
Defining methods with menu in Ruby
问题
这是完整的代码。我希望这足够清楚。如果我不够具体,我很抱歉。
要将这三个命令分成三个不同的方法,并且思考是否正确,可以按照以下方式进行重构:
- 将命令行参数解析的部分移动到一个单独的方法中,以便清晰地处理参数解析逻辑:
def parse_arguments(arguments)
ARGV << "-h" if ARGV.empty?
@options = OpenStruct.new
args = OptionParser.new do |args|
args.banner = "Usage: #{__FILE__} [options]"
args.on("-s", "--set-host=HOST", String, "The Host To Connect To") do |set_host|
@options.set_host = set_host
end
args.on("-u", "--username=USERNAME", String, "Authenticate With A Username") do |username|
@options.username = username
end
args.on("-p", "--password=PASSWORD", String, "Authenticate With A Password") do |password|
@options.password = password
end
args.on("-w", "--wharf=WHARF", Integer, "Specify The Wharf (Port) The Service Is Running") do |wharf|
@options.wharf = wharf
end
args.on("-m", "--mkdir=CREATE DIRECTORY", String, "Create A Directory") do |mkdir|
@options.mkdir = mkdir
end
args.on("-h", "--help", "Show Help And Exit") do
puts args
exit
end
begin
args.parse!(arguments)
rescue OptionParser::MissingArgument => error
puts "[!] ".red + error.message.bold
exit
rescue OptionParser::InvalidOption => error
puts "[!] ".red + error.message.bold
exit
end
end
end
- 将与SFTP连接和目录创建相关的代码分别移动到两个不同的方法中:
def connect_to_sftp
Net::SFTP.start(@options.set_host, @options.username, :password => @options.password, :port => @options.wharf) do |sftp|
create_directory(sftp) if @options.mkdir
end
end
def create_directory(sftp)
sftp.mkdir!(@options.mkdir)
output("Creating Directory => #{@options.mkdir}")
end
- 最后,修改
run
方法,使其分别调用上述三个方法:
def run(arguments)
parse_arguments(arguments)
connect_to_sftp
output("Exiting at => (#{Time.now})")
end
这样,你的代码会更清晰,每个方法都有自己的职责。
英文:
I have this code just for demonstration in favor for my question.
How can I sort these three commands into three different methods? And also am I thinking right? I already wrote the menu, and I'm starting to grasp Ruby so I would like to learn more.
require 'net/sftp'
require 'ostruct'
require 'optparse'
class Sftp
def parse(arguments)
ARGV << "-h" if ARGV.empty?
@options = OpenStruct.new
args = OptionParser.new do |args|
args.banner = "Usage: #{__FILE__} [options]"
args.on("-s", "--set-host=HOST", String,
"The Host To Connect To") do |set_host|
@options.set_host = set_host
end
args.on("-u", "--username=USERNAME", String,
"Authenticate With A Username") do |username|
@options.username = username
end
args.on("-p", "--password=PASSWORD", String,
"Authenticate With A Password") do |password|
@options.password = password
end
args.on("-w", "--wharf=WHARF", Integer,
"Specify The Wharf (Port) The Service Is Running") do |wharf|
@options.wharf = wharf
end
args.on("-m", "--mkdir=CREATE DIRECTORY", String,
"Create A Directory") do |mkdir|
@options.mkdir = mkdir
end
args.on("-h", "--help", "Show Help And Exit") do
puts args
exit
end
begin
args.parse!(arguments)
rescue OptionParser::MissingArgument => error
puts "[!] ".red + error.message.bold
exit
rescue OptionParser::InvalidOption => error
puts "[!] ".red + error.message.bold
exit
end
def connect(arguments)
Net::SFTP.start(@options.set_host, @options.username, :password => @options.password, :port => @options.wharf) do |sftp|
mkdir(sftp) if @options.mkdir
end
output("Exiting at => (#{Time.now})")
end
def run(arguments)
parse(arguments)
connect(arguments)
end
private
def mkdir(sftp)
sftp.mkdir!(@options.mkdir)
output("Creating Directory => #{@options.mkdir}")
end
def output(string)
puts "----------------------------------------------------------"
puts " #{string}"
puts "----------------------------------------------------------"
end
end
end
end
sftp = Sftp.new
sftp.run(ARGV)
This is the full code. I hope this is clear enough. Sorry if I wasn't very specific.
答案1
得分: 1
以下是代码的翻译部分:
我的第一个重构步骤会像这样:
require 'net/sftp'
require 'ostruct'
require 'optparse'
class Sftp
def run(arguments)
parse(arguments)
connect(arguments)
end
private
def parse(arguments)
ARGV << "-h" if ARGV.empty?
@options = OpenStruct.new
args = OptionParser.new do |args|
args.banner = "用法: #{__FILE__} [选项]"
args.on("-s", "--set-host=HOST", String, "要连接的主机") do |set_host|
@options.set_host = set_host
end
args.on("-u", "--username=USERNAME", String, "使用用户名进行身份验证") do |username|
@options.username = username
end
args.on("-p", "--password=PASSWORD", String, "使用密码进行身份验证") do |password|
@options.password = password
end
args.on("-w", "--wharf=WHARF", Integer, "指定服务运行的码头(端口)") do |wharf|
@options.wharf = wharf
end
args.on("-m", "--mkdir=CREATE DIRECTORY", String, "创建目录") do |mkdir|
@options.mkdir = mkdir
end
args.on("-h", "--help", "显示帮助并退出") do
puts args
exit
end
begin
args.parse!(arguments)
rescue OptionParser::MissingArgument => error
puts "[!] ".red + error.message.bold
exit
rescue OptionParser::InvalidOption => error
puts "[!] ".red + error.message.bold
exit
end
end
end
def connect(arguments)
Net::SFTP.start(@options.set_host, @options.username, :password => @options.password, :port => @options.wharf) do |sftp|
mkdir(sftp) if @options.mkdir
rmdir(sftp) if @options.rmdir
erase(sftp) if @options.erase
end
output("退出时间 => (#{Time.now})")
end
def mkdir(sftp)
sftp.mkdir!(@options.mkdir)
output("创建目录 => #{@options.mkdir}")
end
def rmdir(sftp)
sftp.rmdir!(@options.rmdir)
output("删除目录 => #{@options.rmdir}")
end
def erase(sftp)
sftp.remove!(@options.erase)
output("删除文件 => #{@options.erase}")
end
def output(string)
puts "----------------------------------------------------------"
puts " #{string}"
puts "----------------------------------------------------------"
end
end
sftp = Sftp.new
sftp.run(ARGV)
英文:
My first refactoring step would look like this:
require 'net/sftp'
require 'ostruct'
require 'optparse'
class Sftp
def run(arguments)
parse(arguments)
connect(arguments)
end
private
def parse(arguments)
ARGV << "-h" if ARGV.empty?
@options = OpenStruct.new
args = OptionParser.new do |args|
args.banner = "Usage: #{__FILE__} [options]"
args.on("-s", "--set-host=HOST", String, "The Host To Connect To") do |set_host|
@options.set_host = set_host
end
args.on("-u", "--username=USERNAME", String, "Authenticate With A Username") do |username|
@options.username = username
end
args.on("-p", "--password=PASSWORD", String, "Authenticate With A Password") do |password|
@options.password = password
end
args.on("-w", "--wharf=WHARF", Integer, "Specify The Wharf (Port) The Service Is Running") do |wharf|
@options.wharf = wharf
end
args.on("-m", "--mkdir=CREATE DIRECTORY", String, "Create A Directory") do |mkdir|
@options.mkdir = mkdir
end
args.on("-h", "--help", "Show Help And Exit") do
puts args
exit
end
begin
args.parse!(arguments)
rescue OptionParser::MissingArgument => error
puts "[!] ".red + error.message.bold
exit
rescue OptionParser::InvalidOption => error
puts "[!] ".red + error.message.bold
exit
end
end
end
def connect(arguments)
Net::SFTP.start(@options.set_host, @options.username, :password => @options.password, :port => @options.wharf) do |sftp|
mkddir(sftp) if @options.mkdir
rmdir(sftp) if @options.rmdir
erase(sftp) if @options.erase
end
output("Exiting at => (#{Time.now})")
end
def mkdir(sftp)
sftp.mkdir!(@options.mkdir)
output("Creating Directory => #{@options.mkdir}")
end
def rmdir(sftp)
sftp.rmdir!(@options.rmdir)
output("Deleting Directory => #{@options.rmdir}")
end
def erase(sftp)
sftp.remove!(@options.erase)
output("Deleting File => #{@options.erase}")
end
def output(string)
puts "----------------------------------------------------------"
puts " #{string}"
puts "----------------------------------------------------------"
end
end
sftp = Sftp.new
sftp.run(ARGV)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论