简体   繁体   English

Ruby Thor命令行工具命令

[英]Ruby thor command line tool commands

I am planning on making my first command line tool and was wondering about how the naming and commands work. 我正计划制作我的第一个命令行工具,并且想知道命名和命令的工作方式。

I would like my tool to function similarly to Git, in the sense that you just install it, then to run commands you just enter git clone or git commit . 我希望我的工具的功能类似于Git,从某种意义上说,您只需安装它,然后运行命令,只需输入git clonegit commit In many of the examples I have seen the tools are something like thor foo:bar or ./foo.rb bar . 在许多示例中,我已经看到工具类似于thor foo:bar./foo.rb bar

My main question is how can I make it so if my tools name is Foo , and the command in my tool is bar , all the user has to do is run Foo bar in the command line. 我的主要问题是如何做到这一点,如果我的工具名称是Foo ,并且工具中的命令是bar ,那么用户要做的就是在命令行中运行Foo bar

In the case of git clone the way it works is that git program is executed and clone is passed to it as a parameter. 对于git clone ,它的工作方式是执行git程序并将clone作为参数传递给它。 So, if you have several scripts that should be started in a similar manner, you can create a small launcher. 因此,如果您有几个应该以类似方式启动的脚本,则可以创建一个小型启动器。 This code is in bash(but the script is sh compatible, so you can safely change the shebang to /bin/sh ). 该代码在bash中(但是脚本与sh兼容,因此您可以安全地将shebang更改为/bin/sh )。 It is very easy to write the same thing in any other language. 用任何其他语言编写相同的东西非常容易。

#!/bin/bash

command=$1
shift

case $1 in    
    cmd1)
        echo "Executing command #1"
        ./smallcmd1 "$@"
        ;;
    cmd2)
        echo "Executing command $command"
        ./smallcmd2 "$@"
        ;;
    '')
        echo 'No option specified'
        exit 1
        ;;

    *) echo "Invalid option"
        exit 1
        ;;
esac
exit $?

Where smallcmd s are your secondary command scripts or programs. 其中smallcmd是您的辅助命令脚本或程序。

Now you can execute it: 现在您可以执行它:

./mycommand smallcmd1

Additional parameters can be passed as well. 也可以传递其他参数。

If you place this script into any $PATH directory then you can omit ./ like so: 如果将此脚本放置在任何$ PATH目录中,则可以省略./如下所示:

mycommand smallcmd1

Making an executable with Thor is really simple. 使用Thor编写可执行文件非常简单。 All you have to do is: 您要做的就是:

  • include the ruby shebang line. 包括红宝石shebang线。
  • require "thor" in your script. 在脚本中要求“ thor”。
  • define your Thor class. 定义您的雷神类。
  • add #{YourThorClassname}.start to the bottom of your script. #{YourThorClassname}.start添加到脚本的底部。

Example: my-cli.rb 示例:my-cli.rb

#!/usr/bin/env ruby

require "thor"

class MyCLI < Thor
  desc "foo", "Prints foo"
  def foo
    puts "foo"
  end
end

MyCLI.start

Make the script executable: 使脚本可执行:

chmod a+x my-cli.rb

Now you can type: 现在您可以输入:

./my-cli.rb foo

You can find a similar example and more help in the Thor Wiki . 您可以在Thor Wiki中找到类似的示例和更多帮助。

You can even rename the file to my-cli (without the .rb extension) and it still works because of the ruby shebang inside the file. 您甚至可以将文件重命名为my-cli (不带.rb扩展名),并且由于文件中的ruby shebang而仍然有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM