Skip to content

Use Click to parse command-line arguments in bimatrix.py - #16

Merged
rahulsavani merged 10 commits into
gambitproject:mainfrom
nataliemes:refactor/click-cli-bimatrix
Jul 30, 2026
Merged

Use Click to parse command-line arguments in bimatrix.py#16
rahulsavani merged 10 commits into
gambitproject:mainfrom
nataliemes:refactor/click-cli-bimatrix

Conversation

@nataliemes

@nataliemes nataliemes commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

Changes similar to lemke.py

  • Instead of using global variables, main() accepts arguments (populated by Click), which are then passed to solvers.
  • Input filename is now a required argument. If the file does not exist, is not readable, or a directory is passed instead of a file, a CLI error is shown automatically.
  • -h was added as a help option name.
  • Options follow POSIX convention: single-character flags use a single dash, while multi-character flags use double dashes.

Other changes

Library usage:

  • When using LH() as a library function, it is now mandatory to specify the labels string (it used to be empty by default).

  • tracing() was replaced with 2 methods: trace_uniform_prior() with no arguments, and trace_random_priors(trace, seed=None, accuracy=1000) (where trace is the number of random priors).

    Before Now
    game.tracing(trace=0) game.trace_uniform_prior()
    game.tracing(trace=0, seed=1) no longer allowed
    game.tracing(trace=20) game.trace_random_priors(trace=20)
    game.tracing(trace=20, seed=1) game.trace_random_priors(trace=20, seed=1)
  • Instead of just returning when negative number of priors is passed to tracing(), trace_random_priors() raises a ValueError.

  • To indicate that a random seed shouldn't be used in trace_random_priors(), seed is set to None (instead of a negative number like before). Negative seed values are now accepted.

CLI usage:

  • --decimals and --accuracy now have range checks.

  • bimatrix command is split into 2 subcommands: lh and trace.
    trace also has 2 subcommands: uniform (uses a uniform prior) and random (uses random priors, by default 1).

    Before Now
    bimatrix (default file name was used) no longer allowed (file name is required)
    bimatrix game_file (lh and tracing didn't run) no longer allowed
    bimatrix game_file -LH (uses all labels) bimatrix lh game_file
    bimatrix game_file -LH 1-2 (labels 1, 2) bimatrix lh game_file --labels 1-2
    bimatrix game_file -LH -seed 1 no longer allowed (Lemke-Howson doesn't use a random seed)
    bimatrix game_file -trace bimatrix trace uniform game_file
    bimatrix game_file -trace -seed 1 no longer allowed (tracing with a uniform prior doesn't use a random seed)
    bimatrix game_file -trace 1 bimatrix trace random game_file, or bimatrix trace random game_file --priors 1
    bimatrix game_file -trace 10 -seed 1 bimatrix trace random game_file --priors 10 --seed 1
    bimatrix game_file -LH -trace no longer possible directly, since they're now separate commands

Tests:

  • Tests were added for bimatrix.py.
  • Correctness tests for the tracing procedure only used random priors, but now tests also use uniform priors.

@rahulsavani
rahulsavani self-requested a review July 24, 2026 04:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors bimatrix.py’s command-line interface to use Click, aligning it with the project’s other CLIs and improving argument validation and subcommand structure.

Changes:

  • Replaces manual sys.argv parsing with a Click-based CLI entry point (bimatrix), including -h help alias and POSIX-style options.
  • Splits the CLI into lh and trace subcommands with shared “common options” and per-subcommand options.
  • Updates library entry points to accept explicit parameters (e.g., tracing(trace, seed=None, accuracy=1000)) and raises on invalid negative priors.
Comments suppressed due to low confidence (1)

src/lemke/bimatrix.py:173

  • LH("") currently results in an empty label list (because rangesplit("") returns []), so the method silently finds no equilibria. Since the CLI default uses "1-" to mean “all labels”, it’s safer to treat an empty string the same way (or raise a clear error).
    def LH(self, LHstring):
        m = self.A.numrows
        n = self.A.numcolumns
        lhset = {}  # dict of equilibria and list by which label found
        labels = rangesplit(LHstring, m + n)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/lemke/bimatrix.py
Comment thread src/lemke/bimatrix.py Outdated
Comment thread src/lemke/bimatrix.py Outdated
Comment thread src/lemke/bimatrix.py
Comment on lines +275 to +279
@click.group(
context_settings={"help_option_names": ["-?", "-h", "--help"]},
)
def main():
processArguments()
printglobals()

G = bimatrix(gamefilename)
print(G)
G.LH(LHstring)
G.tracing(trace)


if __name__ == "__main__":
main()
"""Find Nash equilibria of a bimatrix game."""
Comment thread src/lemke/bimatrix.py Outdated
@rahulsavani

Copy link
Copy Markdown
Member

@nataliemes Could you resolve the merge conflicts please.

@rahulsavani rahulsavani left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@rahulsavani
rahulsavani merged commit 1bd8b63 into gambitproject:main Jul 30, 2026
8 checks passed
@nataliemes
nataliemes deleted the refactor/click-cli-bimatrix branch July 31, 2026 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants