{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Command Line Interface\n", "\n", "This notebook illustrates the command line interface (CLI) of the larvaworld package\n", "\n", "The CLI entry point / shell command is `larvaworld` and can be run on a terminal along with additional arguments. \n", "\n", "Here we will run it from within the notebook using `!`.\n", "\n", "The `--help`/`-h` flag displays a help message.\n", "\n", "The `-verbose` flag controls the verbosity of the output\n", "\n", "The `--show_parser_args`/`-parsargs` flag shows the parsed argument namespace" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Display the help message for larvaworld\n", "!larvaworld -h" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Control the verbosity of the output\n", "!larvaworld -verbose 0" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Show the parsed argument namespace\n", "!larvaworld -parsargs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is only a single required positional argument that defines the simulation mode.\n", "\n", "This takes one of several predefined values (see help message above) and should be provided just after the command.\n", "\n", "Once the simulation mode is defined the available arguments can be set. Some of them are common but others diverge among modes.\n", "\n", "The help message can illustrate this :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Display the help message for a given simulation mode\n", "!larvaworld Exp -h" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Display the help message for a given simulation mode\n", "!larvaworld Ga -h" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Argument parser\n", "\n", "What arguments are there available in CLI?\n", "\n", "To answer this let's have a look at what's happening behind the scenes. The `ParserArgumentDict` class creates the available arguments for a specified input configuration class. Let's see some examples : " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from larvaworld.lib.param import RuntimeOps, SimOps\n", "from larvaworld.cli.argparser import ParserArgumentDict\n", "from larvaworld.lib.screen import ScreenOps\n", "from larvaworld.lib import reg\n", "\n", "# Available simulation arguments\n", "sim_kws = ParserArgumentDict.from_param(d0=SimOps)\n", "sim_kws.parsargs.keylist.sorted" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Available visualization arguments\n", "screen_kws = ParserArgumentDict.from_param(d0=ScreenOps)\n", "screen_kws.parsargs.keylist.sorted" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Available GA-mode arguments\n", "GAselector = ParserArgumentDict.from_param(d0=reg.gen.GAselector)\n", "GAevaluation = ParserArgumentDict.from_param(d0=reg.gen.GAevaluation)\n", "\n", "print(GAselector.parsargs.keylist.sorted)\n", "print(GAevaluation.parsargs.keylist.sorted)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Available replay-mode arguments\n", "Replay = ParserArgumentDict.from_param(d0=reg.gen.Replay)\n", "Replay.parsargs.keylist.sorted" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's see the overarching argument parser used in CLI" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from larvaworld.cli.argparser import SimModeParser\n", "\n", "# Initialize the parser\n", "P = SimModeParser()\n", "\n", "# The grouped arguments\n", "print(P.parsers)\n", "\n", "# The individual arguments\n", "# print(P.__dict__)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "larvaworld_autoversioning", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.13" } }, "nbformat": 4, "nbformat_minor": 2 }