CLI implementation

Command line interface

class pygindex.cli.AccountCommand

Query account details

get()

Get account information

class pygindex.cli.CommandMeta(*args, **kwargs)

Metaclass to create command classes.

Currently the only purpose of this metaclass is to inject default class property that defines object cli name.

Thus all classes that inherit from this metaclass are going to have cli_name property set to the lowercase name of the class, unless they override it.

class pygindex.cli.GenericCommand

Class to group and identify all CLI command classes.

This is a simple approach to have “pluggable” architecture, and at this time this class is not responsible for anything else.

The docstrings in child classes are important:
  • Class docstring is used as a CLI group description

  • Method decorated with cli_command() docstring is used as action group description

class pygindex.cli.InstrumentCommand

Instruments

get()

Get instrument details

search()

Search for instrument

class pygindex.cli.PositionsCommand

Manage positions

close()

Close position

get()

Get all positions

open()

Open position

pygindex.cli.app()

Main application function.

Build parser, create command dispatch map and call the command specified on the command line.

pygindex.cli.build_dispatch_map(parser: argparse.Namespace)dict

Create callable dispatch map.

Parameters

parser – Instance of initialised argparse

Returns

Map of callables for each CLI (object, action) pair

pygindex.cli.dispatch_command(args: argparse.Namespace, dispatch_map: dict)

Given a parsed arguments object and a dispatch map, call relevant callable for object and command pair.

Pass on all other available arguments to the callable as a dictionary, but remove object and command entries.

Parameters
  • args – Object containing parsed arguments

  • dispatch_map – Dictionary with callables for (object, command) pairs

pygindex.cli.get_api_config(platform: str = None)pygindex.models.IGAPIConfig

Create API configuration object

Parameters

platform – Optional platform selector, defaults to None in which case a setting from configuration file will be used.

Returns

IG API configuration object

pygindex.cli.get_auth_config(platform: str = None)pygindex.models.IGUserAuth

Create authentication object

Parameters

platform – Optional platform selector, defaults to None in which case a setting from configuration file will be used.

Returns

IG Authentication object

pygindex.cli.get_config()dict

Build configuration dictionary

Returns

Dictionary containing configuration settings

pygindex.cli.init_parser() → Tuple[argparse.ArgumentParser, argparse._SubParsersAction]

Create an instance of argparse.ArgumentParser

Returns

A tuple of created parser and subparser instances

pygindex.cli.parse_args(parser: argparse.ArgumentParser)argparse.Namespace

Parse command line arguments using provided parser.

Parameters

parser – Instance of initialised argparse

Returns

Namespace generated by parse_args()

pygindex.cli.register_command_parsers(cls: type, root_subparser: argparse.Namespace)dict

Discover classes implementing command line objects and actions. Create new argparse parser for each discovered CLI object class. Register the created parser under the root_subparser. Create new subparser to hold all commands, and create new parsers for each command. Register command callables for every action discovered.

Parameters
  • cls – Class that is used to discover all CLI object classes

  • root_subparser – Base subparser where all command and action parsers will be registered

Returns

A map of callables for each (object, action)