Glow Command line internals
A command line tool has a few parts:
- parser to parse command line options.
- handlers to deal with the parsed datastructure.
Where exactly is
glow cli defined?
We can invoke it with
./glow, which invokes
main.ss. In there, it runs the following:
(current-program "glow") (initialize-glow-path!) (define-multicall-main)
Let’s analyze this section by section:
(current-program "glow") ;; is evaluated to ((make-parameter ) "glow")
If we look at Gambit - make-parameter, we know that the environment of execution has 2 parts:
We first access
local environment, if a variable can’t be found, we then try
global. A new cell is instantiated and bound to a new parameter object. The content of this cell is
. Whenever an argument, in this case
"glow", is passed to the parameter object the contents get updated. If no arguments are passed, this just returns the cell contents.
> (define radix (make-parameter 10)) > (radix) 10 > (radix 2) > (radix) 2 The top-level module for `glow-cli` is located in `cli/interaction.ss` What is `define-multicall-main`? It is called `make-parameter`, because the associated variable is dynamically bound! Reference: https://srfi.schemers.org/srfi-39/srfi-39.html Also note: This is not thread safe for gambit according to be above srfi? Q: What's the point though? Can't we just do `set!` against a variable, doesn't that accomplish the same thing? A: This is for `dynamic` variables. ### define-multicall-main First let's decrypt what is a <d223d1a2>. Next we can see we define a `main` entrypoint, which is multicall, `gxi` uses this to know the program entrypoint. Based on that this uses `call-entrypoint` which calls `call-entrypoint/internal`. This then gets the entrypoints by the `command`, i.e. the first argument. It then calls that command. Next, we observe in `cli/interaction.ss`, and other places we use `define-entry-point` to start an interaction. ## What do we use to construct our CLI parser? We use `gerbil-poo/cli` which uses `getopt`. ## How does `gerbil-poo/cli` work? Its interface is located in `gerbil-poo/cli`. It uses `make-options`