Ruby libraries for command-prompt-based applications - ruby

I'm looking to create a Ruby application with a command-prompt-based interface. By "command-prompt-interface," I mean something similar to irb, but note that I do not mean actually executing Ruby code. The gems I'm finding via Google seem to be more suited to CLIs like git, not an actual prompt like irb.
I need to be able to define commands and handlers for those commands, so something like:
(prompt) helloworld
Hello World
(prompt)
Validation and a built-in help system would also be good extras, as would the ability to execute a single command from the command line (calling it from the shell cliapp.rb -c helloworld).
I'm able to create this from scratch, but if there are any libraries available I'd prefer to use that rather than reinventing the wheel.

You may want to try Methadone, which was created by David Bryant Copeland, the author of the book "Build Awesome Command-Line Apps in Ruby". You may want to check out the book as well.
See this blog post for more information.

Related

Can Ruby interact with my root privileges?

This is my first question here, please tell me if I can in some way tag it better or make a better question :)
I'm trying to make a ruby script to install some packages and edit some configurations after I format a computer. I use Manjaro Linux, and my script can already install the official Arch packages, but when it comes to AUR (with Yay) I sometimes receive an error message, saying that Yay can't run as sudo. Also I saw that some people can integrate their Shell with other scripting languages, I started this, but still need to type my Root password more than I want to.
I searched for help on this and found that Python have a library called Pexpct, but didn't found any Ruby alternatives to it. I saw somethings related to Ruby's Expect and IO, but couldn't understand how and when to use it.
programs = [
"zsh",
"zathura",
"zathura-cb",
"zathura-djvu",
"zathura-pdf-mupdf",
"zathura-ps",
"texlive-most",
"texlive-lang",
"geogebra",
"vim",
"yay",
"adobe-source-code-pro-fonts",
"firefox-developer-edition"
]
#Array iteration to install Arch official repo's packages
programs.each do |name|
system ("pacman -Sq #{name} --noconfirm")
end
I want to know if in some way can this code inside the programs block insert my password. I know it has a lot of security problems, but is on this case for this one snippet of code and study cases. I won't use it on other scripts.

Where is the per-method documentation for ruby

I am looking for the primary documentation for ruby functions. As in, the full documentation for the system command.
For example, I have seen one guy who is using the system command with redirection arguments at Running a command from Ruby displaying and capturing the output but someone else who doesn't know that this exists Getting output of system() calls in Ruby
I don't care about the answer to that specific question, but when I search https://www.ruby-lang.org/en/documentation/ which seems authoritative, all I can find are links to third party "guides" or conceptual learning resources. What I need is the ruby equivalent of PHP's function reference at https://php.net/manual/en/funcref.php Or perldoc -f for perl.
http://ruby-doc.org
Packages are organized into "core" and "std-lib".
For example, here is the documentation for the Kernel#system method.

functional testing for vagrant puppet provisioning (or running a CLI test suite)

I'm looking for a way to automate functional testing of vagrant provisioning scripts (using puppet and shell scripts). please notice: I'm asking about functional testing and not unittesting the puppet modules, the puppet catalog etc.
Is there a recommended way to do that? maybe something along python doctests: "with this input this should be the CLI output". Searched around but couldn't find a working example or a recommanded tool.
If there isn't any vagrant/puppet sanctioned solution for this, is there a simple CLI testing tool? where I can make asserts about certain outputs vs certain inputs?
I saw this related question but it didn't really get a good answer
Maybe serverspec would suite your needs. In particular, with the command resource type, which you can read about here, you should be able to do exactly what you've described.
Note that in addition you can use it to test other resources like processes or opened ports without having to deal with ad-hoc command line scripts.

Perform SVN operations through a ruby script on windows/osx

I am a newbie and need to perform some basic SVN operations (like get info of the repository, add, commit, update etc) through a ruby script, on both windows/osx.
I searched the forum and internet, only to find partial and confusing answers related to SWIG ruby-subversion bindings etc, but none of them was well documented or simple enough to use [Also, most of these questions and answers are pretty old].
I am assuming that a simpler way to perform SVN operations through a ruby script should exist by now. Correct me if my assumption is wrong.
Also, is it worth the hassle to install SWIG bindings, understand 'svn/core' library and use it with a gem (like svn_wc) OR better to simply call svn command line commands from ruby? Is there a massive difference in performance for these approaches?
Any help will be much appreciated.
A simple way is to use SVN command line tools from Ruby.
Say you are in a sandbox and you have a script 'svn-get_uuid'.
It could look like this:
$ cat svn-get_uuid
#!/usr/bin/ruby
uuid = `svn info`.lines.grep /^UUID/
uuid = uuid[0].chomp.sub /^.*: (.+)/, '\1'
print "{#{uuid}}\n"
$ ./svn-get_uuid
{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
May be you can do it faster using the svn-bindings, but working this way is OK for my daily work.

mirroring Terminal with gets Ruby

There may be a good gem out there for this, but I've run out of Google.
I'm creating a command line interface with a basic gets each inputed command. I also want to have basic terminal features like hitting the up arrow and having it cycle the history. Are there gems which mimic this or is there a simple way to implement it?
Not to self-promote, but my friend and I wrote a project that is mostly functional with this goal. Check it out here: https://github.com/jamez01/arsh
It's also a packaged gem that you can install.
Some time ago I did a "smart telnet" using "Readline". I even enabled autocompletion.
It has all the cool features like up down arrow, autocompletion, ...
Take a look at it

Resources