Rewrite option parsing using clim and add the admin subcommand

This commit is contained in:
Alex Ling 2020-05-30 15:14:39 +00:00
parent dd01e632a2
commit 651bd17612
3 changed files with 73 additions and 22 deletions

View File

@ -2,7 +2,7 @@ version: 1.0
shards:
ameba:
github: crystal-ameba/ameba
version: 0.12.0
version: 0.12.1
archive:
github: hkalexling/archive.cr
@ -12,6 +12,10 @@ shards:
github: schovi/baked_file_system
version: 0.9.8
clim:
github: at-grandpa/clim
version: 0.12.0
db:
github: crystal-lang/crystal-db
version: 0.9.0

View File

@ -23,3 +23,5 @@ dependencies:
github: hkalexling/archive.cr
ameba:
github: crystal-ameba/ameba
clim:
github: at-grandpa/clim

View File

@ -2,31 +2,76 @@ require "./config"
require "./server"
require "./mangadex/*"
require "option_parser"
require "clim"
VERSION = "0.4.0"
MANGO_VERSION = "0.4.0"
config_path = nil
macro common_option
option "-c PATH", "--config=PATH", type: String,
desc: "Path to the config file"
end
OptionParser.parse do |parser|
parser.banner = "Mango e-manga server/reader. Version #{VERSION}\n"
macro throw(msg)
puts "ERROR: #{{{msg}}}"
puts
puts "Please see the `--help`."
exit 1
end
parser.on "-v", "--version", "Show version" do
puts "Version #{VERSION}"
exit
class CLI < Clim
main do
desc "Mango - Manga Server and Web Reader. Version #{MANGO_VERSION}"
usage "mango [sub_command] [options]"
help short: "-h"
version "Version #{MANGO_VERSION}", short: "-v"
common_option
run do |opts|
Config.load(opts.config).set_current
MangaDex::Downloader.default
server = Server.new
server.start
end
sub "admin" do
desc "Run admin tools"
usage "mango admin [tool]"
help short: "-h"
run do |opts|
puts opts.help_string
end
sub "user" do
desc "User management tool"
usage "mango admin user [arguments] [options]"
help short: "-h"
argument "action", type: String,
desc: "Action to make. Can be add/delete/update", required: true
argument "username", type: String,
desc: "Username to update or delete"
option "-u USERNAME", "--username=USERNAME", type: String,
desc: "Username"
option "-p PASSWORD", "--password=PASSWORD", type: String,
desc: "Password"
option "--admin", desc: "Admin flag", type: Bool, default: false
common_option
run do |opts, args|
Config.load(opts.config).set_current
case args.action
when "add"
throw "Options `-u` and `-p` required." if opts.username.nil? ||
opts.password.nil?
when "delete"
throw "Argument `username` required." if args.username.nil?
when "update"
throw "Argument `username` required." if args.username.nil?
else
throw "Unknown action \"#{args.action}\"."
end
end
end
parser.on "-h", "--help", "Show help" do
puts parser
exit
end
parser.on "-c PATH", "--config=PATH",
"Path to the config file. " \
"Default is `~/.config/mango/config.yml`" do |path|
config_path = path
end
end
Config.load(config_path).set_current
MangaDex::Downloader.default
server = Server.new
server.start
CLI.start(ARGV)