Rand Stats

Getopt::Long::Grammar

zef:antononcube

Getopt::Long::Grammar

Raku package for parsing Getopt specs and Python CLI specs.

Motivation

Given a Jupyter chatbook made with the Python kernel I want to be able to switch to a Raku kernel for that chatbook and use the chat cells as they are, without changing the "magic specs." This means, being able to parse the magic argument specs in the way the Jupyter Python Kernel uses them. (See "IPython extensions" and "argparse".)


Installation

From Zef ecosystem:

zef install Getopt::Long::Grammar

From GitHub:

zef isntall https://github.com/antononcube/Raku-Getopt-Long-Grammar.git

Usage examples

Load the package and parse a typical magic command:

use Getopt::Long::Grammar;

my $cmd = '%%chat_meta cw1 --api_key=cd77fdfw1 --prompt';

getopt-parse($cmd);
# 「%%chat_meta cw1 --api_key=cd77fdfw1 --prompt」
#  getopt-command => 「%%chat_meta」
#  getopt-args => 「cw1 --api_key=cd77fdfw1 --prompt」
#   argument-list => 「cw1」
#    argument => 「cw1」
#   option-list => 「--api_key=cd77fdfw1 --prompt」
#    option => 「--api_key=cd77fdfw1」
#     option-pair => 「api_key=cd77fdfw1」
#      option-name => 「api_key」
#      option-value => 「cd77fdfw1」
#    option => 「--prompt」
#     option-pair => 「prompt」
#      option-name => 「prompt」

Interpret:

getopt-interpret($cmd);
# {arguments => (cw1), command => %%chat_meta, options => {api_key => cd77fdfw1, prompt => True}}