I am writing a CLI utility/framework as an Umbrella app. The concept is to have a main command, we
. I would like the we
command to take some core arguments, e.g.:
Available subcommands are:
we -h Display this help message
we -v Display version
we config Writes user profile ~/.weconf
-l Displays user profile
we update Updates we
we uninstall Remove we
Any other argument list that is given I’d like to hand off to other apps in the umbrella. e.g.:
./we tag foo -a bar
internally would parse out tag foo -a bar
and hand off foo -a bar
to the Tag app.
I am having difficulty understanding the OptionParser. I have 2 parts to the question I guess.
-
How best to separate in a dynamic function the first argument if it doesn’t match in a case as the “subcommand”
-
Is it possible to “Constantize” (Like in Rails) a the string that is parsed out and execute it as an app call? e.g.: Tag.run(remaining_args_from_parse)
defmodule We.CLI do
@moduledoc “”"
Mainwe
cli toolusage: we [ | -hv]
Available subcommands are:
we -h Display this help message
we -v Display version
we config Writes user profile ~/.wetools
-l Displays user profile
we update Updates WeTools
we uninstall Remove WeTools
“”"
import Loki.Shell
@version “1.0.0”def main(argv) do
argv
|> parse_args
|> process
enddef parse_args(argv) do
switches = [help: :boolean, version: :boolean]
aliases = [h: :help, v: :version]parse = OptionParser.parse(argv, switches: switches, aliases: aliases) case parse do {[help: true], _, _} -> :help {[version: true], _, _} -> :version {[update: true], _, _} -> :update {_,["config"], _} -> :config {_,["update"], _} -> :update {_,["uninstall"], _} -> :uninstall _ -> :help end
end
def process(:uninstall) do
say “execute uninstall function”
System.halt(0)
enddef process(:update) do
say “execute update function”
System.halt(0)
enddef process(:config) do
say “execute config function”
System.halt(0)
enddef process(:version) do
say “Version: #{@version}”
System.halt(0)
enddef process(:help) do
say @moduledoc
System.halt(0)
end
end
Also, this is the first thing I’ve done in Elixir so forgive me if I am not following the correct patterns.
Thank you in advanced.