Skip to content

Commit

Permalink
Added command line arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jo1gi committed Apr 7, 2021
1 parent 1654678 commit b524aaf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
24 changes: 24 additions & 0 deletions src/args.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "option_parser"

class Args
getter config : String | Nil
getter url : String = ""

def initialize()
OptionParser.parse do |parser|
parser.banner = "Usage: openurl [options] URL"
parser.on("-c CONFIG", "--config CONFIG", "Path to alternative config file") { |c| @config = c }
parser.on("-h", "--help", "Show help menu") do
puts parser
exit
end
parser.unknown_args() do |args|
if args.size != 0
@url = args[0]
else
abort "An url is required"
end
end
end
end
end
29 changes: 16 additions & 13 deletions src/openurl.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "yaml"
require "./args"

# Stores all of the available rules for urls to be tested against
tests = {
Expand Down Expand Up @@ -60,17 +61,19 @@ def find_command(url : String, config, tests)
end
end

if ARGV.size > 0
config = load_config(find_config)
result = find_command(ARGV[0], config, tests)
if result.nil?
abort "Could not find matching program"
end
split = result.split(' ')
command = split[0]
args = split[1..]
args.push(ARGV[0])
Process.run(command, args: args)
else
abort "An argument is required"
# Loading arguments
opts = Args.new
# Loading config file
config_path = opts.config || find_config
config = load_config(config_path)
# Finding command
result = find_command(opts.url.not_nil!, config, tests)
if result.nil?
abort "Could not find matching program"
end
# Running command
split = result.split(' ')
command = split[0]
args = split[1..]
args.push(opts.url)
Process.run(command, args: args)

0 comments on commit b524aaf

Please sign in to comment.