【问题标题】:How to default the [Yn] responses for the Ruby "gem clean" command?如何默认 Ruby“gem clean”命令的 [Yn] 响应?
【发布时间】:2011-12-22 18:27:34
【问题描述】:

我经常使用 Ruby gem clean 命令来保持本地 gem 存储库的正常运行。

但是由于依赖问题,很多时候命令会返回提示比如:

XXXXX-1.0.6 depends on [YYYYYY (~> 0.8.4)]
If you remove this gems, one or more dependencies will not be met.
Continue with Uninstall? [Yn]  

虽然这很简单,但它需要人工干预(对于[Yn] 响应),因此这使我无法创建一个简单的cron 脚本来自动执行此过程。

关于如何默认这些gem 提示的响应有什么想法吗?

【问题讨论】:

    标签: ruby automation gem default prompt


    【解决方案1】:

    你应该有一个yes命令,OSX版本是这样说的:

    YES(1)                    BSD General Commands Manual                   YES(1)
    
    NAME
         yes -- be repetitively affirmative
    
    SYNOPSIS
         yes [expletive]
    
    DESCRIPTION
         yes outputs expletive, or, by default, ``y'', forever.
    
    HISTORY
         The yes command appeared in 4.0BSD.
    
    4th Berkeley Distribution        June 6, 1993        4th Berkeley Distribution
    

    所以也许这会起作用:

    yes n | gem clean
    

    gem clean 可能直接从终端而不是标准输入读取。在这种情况下,您可能会遇到expect

    Expect 是一个根据脚本与其他交互式程序“对话”的程序。根据脚本,Expect 知道程序可以预期什么以及正确的响应应该是什么。解释语言提供分支和高级控制结构来指导对话。此外,用户可以在需要时直接进行控制和交互,然后将控制权返回给脚本。

    因此您可以编写一个expect 脚本来根据需要使用“y”或“n”来响应预期的提示。

    【讨论】:

      【解决方案2】:

      这应该可行:

      echo|gem clean
      

      它就像在提示符下按回车一样。 'y' 是默认值,它将运行 gem clean 完成。

      【讨论】:

      • 我刚刚对此进行了测试,但它似乎没有按预期运行。在我的环境中,我在这些情况下给 gem clean 喂一个“n”。所以,我跑了...“echo 'n' | gem clean”,它把它当作 y 并清理掉了 gem。
      • 你是对的 - 但是,我猜它仍然可以解决问题。编辑以减少误导。
      【解决方案3】:

      Thilo 的回答是最简单的。但是,如果您需要使用“n”,那么您就不走运了。一些搜索发现这个线程有一些示例 Java 代码:Java: Detecting user prompt when running a batch script from Java

      我针对这种情况对其进行了一些修改,我认为这将允许您更保守地选择使用“n”作为默认选项。当然,如果您不想让 java 类在您的 cron 作业中运行,则可以将此代码修改为 bash 脚本 - 但我会将其留给更有 bash 经验的人。

      我现在没有简单的方法来测试这段代码,所以请告诉我它的运行情况。 :)

      public static void main(final String... args) throws IOException, InterruptedException {
          final Runtime runtime = Runtime.getRuntime();
          final String command = "..."; // cmd.exe
          final String matchString = "Continue with Uninstall? [Yn] ";
          final String response = "n";
      
          final Process proc = runtime.exec(command, null, new File("."));
      
          final BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
          final BufferedWriter output = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
      
          StringBuilder sb = new StringBuilder();
          char[] cbuf = new char[100];
          while (input.read(cbuf) != -1) {
              sb.append(cbuf);
              if (sb.substring(sb.length() - matchString.length(), sb.length()).equals(matchString)) {
                  output.write(response);
                  output.newLine();
                  output.flush();
              }
          }
          System.out.println(sb);
      }
      

      【讨论】:

        【解决方案4】:

        gem 的最新版本对此进行了标记。例如,要忽略依赖关系,请使用 -I。要自动删除可执行文件,请使用-x。大多数选项相当于将yes 回答到特定类别的提示。在扩展选项名称前添加 no- 以回答“否”而不是“是”。

        Options:
            -a, --[no-]all                   Uninstall all matching versions
            -I, --[no-]ignore-dependencies   Ignore dependency requirements while
                                             uninstalling
            -D, --[no-]check-development     Check development dependencies while uninstalling
                                             (default: false)
            -x, --[no-]executables           Uninstall applicable executables without
                                             confirmation
            -i, --install-dir DIR            Directory to uninstall gem from
            -n, --bindir DIR                 Directory to remove binaries from
                --[no-]user-install          Uninstall from user's home directory
                                             in addition to GEM_HOME.
                --[no-]format-executable     Assume executable names match Ruby's prefix and suffix.
                --[no-]force                 Uninstall all versions of the named gems
                                             ignoring dependencies
                --[no-]abort-on-dependent    Prevent uninstalling gems that are
                                             depended on by other gems.
            -v, --version VERSION            Specify version of gem to uninstall
                --platform PLATFORM          Specify the platform of gem to uninstall
                --vendor                     Uninstall gem from the vendor directory.
                                             Only for use by gem repackagers.
        

        例如,要清除所有宝石,您可以使用 gem uninstall -Iax

        【讨论】:

          猜你喜欢
          • 2012-07-18
          • 1970-01-01
          • 2022-12-15
          • 2012-03-11
          • 2012-03-27
          • 2023-03-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多