在Ruby中使用菜单定义方法

huangapple go评论63阅读模式
英文:

Defining methods with menu in Ruby

问题

这是完整的代码。我希望这足够清楚。如果我不够具体,我很抱歉。

要将这三个命令分成三个不同的方法,并且思考是否正确,可以按照以下方式进行重构:

  1. 将命令行参数解析的部分移动到一个单独的方法中,以便清晰地处理参数解析逻辑:
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
  1. 将与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
  1. 最后,修改 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 &#39;net/sftp&#39;
require &#39;ostruct&#39;
require &#39;optparse&#39;

class Sftp
  
  def parse(arguments)
    ARGV &lt;&lt; &quot;-h&quot; if ARGV.empty?
    @options = OpenStruct.new
	
    args = OptionParser.new do |args|
      args.banner = &quot;Usage: #{__FILE__} [options]&quot;
	  
      args.on(&quot;-s&quot;, &quot;--set-host=HOST&quot;, String,
         &quot;The Host To Connect To&quot;) do |set_host|
         @options.set_host = set_host
      end
	  
      args.on(&quot;-u&quot;, &quot;--username=USERNAME&quot;, String,
         &quot;Authenticate With A Username&quot;) do |username|
         @options.username = username
      end

      args.on(&quot;-p&quot;, &quot;--password=PASSWORD&quot;, String,
         &quot;Authenticate With A Password&quot;) do |password|
         @options.password = password
      end

      args.on(&quot;-w&quot;, &quot;--wharf=WHARF&quot;, Integer,
         &quot;Specify The Wharf (Port) The Service Is Running&quot;) do |wharf|
         @options.wharf = wharf
      end

      args.on(&quot;-m&quot;, &quot;--mkdir=CREATE DIRECTORY&quot;, String,
         &quot;Create A Directory&quot;) do |mkdir|
         @options.mkdir = mkdir
      end

      args.on(&quot;-h&quot;, &quot;--help&quot;, &quot;Show Help And Exit&quot;) do
        puts args
        exit
      end

    begin
      args.parse!(arguments)
    
    rescue OptionParser::MissingArgument =&gt; error
      puts &quot;[!] &quot;.red + error.message.bold
      exit

    rescue OptionParser::InvalidOption =&gt; error
      puts &quot;[!] &quot;.red + error.message.bold
      exit
   end

def connect(arguments)
  Net::SFTP.start(@options.set_host, @options.username, :password =&gt; @options.password, :port =&gt; @options.wharf) do |sftp|
    mkdir(sftp) if @options.mkdir         
  end

  output(&quot;Exiting at =&gt; (#{Time.now})&quot;)
end
  
def run(arguments)
  parse(arguments)
  connect(arguments)
end

private 

def mkdir(sftp)
  sftp.mkdir!(@options.mkdir)
  output(&quot;Creating Directory =&gt; #{@options.mkdir}&quot;)
end

def output(string)
      puts &quot;----------------------------------------------------------&quot;
      puts &quot; #{string}&quot;
      puts &quot;----------------------------------------------------------&quot;
      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 &#39;net/sftp&#39;
require &#39;ostruct&#39;
require &#39;optparse&#39;
class Sftp
def run(arguments)
parse(arguments)
connect(arguments)
end
private
def parse(arguments)
ARGV &lt;&lt; &quot;-h&quot; if ARGV.empty?
@options = OpenStruct.new
args = OptionParser.new do |args|
args.banner = &quot;Usage: #{__FILE__} [options]&quot;
args.on(&quot;-s&quot;, &quot;--set-host=HOST&quot;, String, &quot;The Host To Connect To&quot;) do |set_host|
@options.set_host = set_host
end
args.on(&quot;-u&quot;, &quot;--username=USERNAME&quot;, String, &quot;Authenticate With A Username&quot;) do |username|
@options.username = username
end
args.on(&quot;-p&quot;, &quot;--password=PASSWORD&quot;, String, &quot;Authenticate With A Password&quot;) do |password|
@options.password = password
end
args.on(&quot;-w&quot;, &quot;--wharf=WHARF&quot;, Integer, &quot;Specify The Wharf (Port) The Service Is Running&quot;) do |wharf|
@options.wharf = wharf
end
args.on(&quot;-m&quot;, &quot;--mkdir=CREATE DIRECTORY&quot;, String, &quot;Create A Directory&quot;) do |mkdir|
@options.mkdir = mkdir
end
args.on(&quot;-h&quot;, &quot;--help&quot;, &quot;Show Help And Exit&quot;) do
puts args
exit
end
begin
args.parse!(arguments)
rescue OptionParser::MissingArgument =&gt; error
puts &quot;[!] &quot;.red + error.message.bold
exit
rescue OptionParser::InvalidOption =&gt; error
puts &quot;[!] &quot;.red + error.message.bold
exit
end
end
end
def connect(arguments)
Net::SFTP.start(@options.set_host, @options.username, :password =&gt; @options.password, :port =&gt; @options.wharf) do |sftp|
mkddir(sftp) if @options.mkdir
rmdir(sftp) if @options.rmdir
erase(sftp) if @options.erase         
end
output(&quot;Exiting at =&gt; (#{Time.now})&quot;)
end
def mkdir(sftp)
sftp.mkdir!(@options.mkdir)
output(&quot;Creating Directory =&gt; #{@options.mkdir}&quot;)
end
def rmdir(sftp)
sftp.rmdir!(@options.rmdir)     
output(&quot;Deleting Directory =&gt; #{@options.rmdir}&quot;)
end
def erase(sftp)
sftp.remove!(@options.erase)
output(&quot;Deleting File =&gt; #{@options.erase}&quot;)
end
def output(string)
puts &quot;----------------------------------------------------------&quot;
puts &quot; #{string}&quot;
puts &quot;----------------------------------------------------------&quot;
end
end
sftp = Sftp.new
sftp.run(ARGV)

huangapple
  • 本文由 发表于 2023年2月24日 02:20:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定