run specific version of ruby script if command line argument is entered

lumos

I've read through tons of variations of my question, but I can't seem to find one that really matches up with what I'm trying to do.

I have a script that essentially has two versions: the "default" would run through a series of mandatory methods, and the second version would run through those same methods, then tack on a series of secondary methods. I would like the second version to be triggered from the command line (something like $ script.rb second_version), and the "default" option to be triggered by no argument input at all (since it's going to be used more frequently, so just $ script.rb).

I tried this with ARGV:

second_version = ARGV[0]

if second_version
   second_version_methods
else
  main_version
end

This technically works, but the problem is that the very first method that runs in the main_version script is a method that requires user input. What ends up happening is that the string "second_version" is automatically passed to that method as the value, which I don't want.

Essentially, is there a way to have a command line argument that will only specify a condition to run in a particular way, and then once that's done, it just drops the input? I've looked into optparse but its functionality seems a bit superfluous for this purpose.

Thank you!

brainbag

ARGV isn't readonly, so you could consume the first input like this:

if ARGV[0] == 'second_version'
  ARGV.shift # removes the first element 
  second_version_methods
else
  main_version
end

If you want to keep ARGV intact (which is a better idea), you can do this:

arguments = ARGV.dup

if arguments[0] == 'second_thing'
  arguments.shift
# ...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Require command line argument for script to run

From Dev

A perl command line that prints the perl version it is using to run your script

From Dev

Ruby Script to Command Line Tool

From Dev

Sniff password entered with read and passed as a command line argument

From Dev

Ruby : Pass array in command line argument

From Dev

Check command line argument in Ruby code is present

From Dev

fdisk command run on terminal but not run with ruby script

From Dev

fdisk command run on terminal but not run with ruby script

From Dev

How to run different python scripts from command line by passing the script name as argument

From Dev

How can I pass an array as argument to TCL script command line run?

From Dev

Restrict standard users to run a command with a specific argument

From Java

how to pass a command line argument in the awk script

From Dev

Shell script not picking the command line argument

From Dev

Bash - Command line argument being mutated in script

From Dev

Passing directory to python script as command line argument

From Dev

Multiline script in a python command line argument

From Dev

passing command line argument to gawk script

From Dev

Bash script command line argument to upper case

From Dev

Command line options with argument in shell script

From Dev

Optional command line argument in R script

From Dev

passing '*' as command line argument to shell script

From Dev

Shell script -e command line argument not recognized

From Dev

How to run a specific version of a ruby gem

From Dev

run command line command within ruby, using ruby variable

From Dev

How to run Ruby scripts in Command Line?

From Dev

How to run windows command using ruby script?

From Dev

Parse command line arguments in a Ruby script

From Dev

How to pass parameters to ruby command line script

From Dev

No return on command line when running Ruby script

Related Related

  1. 1

    Require command line argument for script to run

  2. 2

    A perl command line that prints the perl version it is using to run your script

  3. 3

    Ruby Script to Command Line Tool

  4. 4

    Sniff password entered with read and passed as a command line argument

  5. 5

    Ruby : Pass array in command line argument

  6. 6

    Check command line argument in Ruby code is present

  7. 7

    fdisk command run on terminal but not run with ruby script

  8. 8

    fdisk command run on terminal but not run with ruby script

  9. 9

    How to run different python scripts from command line by passing the script name as argument

  10. 10

    How can I pass an array as argument to TCL script command line run?

  11. 11

    Restrict standard users to run a command with a specific argument

  12. 12

    how to pass a command line argument in the awk script

  13. 13

    Shell script not picking the command line argument

  14. 14

    Bash - Command line argument being mutated in script

  15. 15

    Passing directory to python script as command line argument

  16. 16

    Multiline script in a python command line argument

  17. 17

    passing command line argument to gawk script

  18. 18

    Bash script command line argument to upper case

  19. 19

    Command line options with argument in shell script

  20. 20

    Optional command line argument in R script

  21. 21

    passing '*' as command line argument to shell script

  22. 22

    Shell script -e command line argument not recognized

  23. 23

    How to run a specific version of a ruby gem

  24. 24

    run command line command within ruby, using ruby variable

  25. 25

    How to run Ruby scripts in Command Line?

  26. 26

    How to run windows command using ruby script?

  27. 27

    Parse command line arguments in a Ruby script

  28. 28

    How to pass parameters to ruby command line script

  29. 29

    No return on command line when running Ruby script

HotTag

Archive