Giter Club home page Giter Club logo

Comments (7)

BrianPugh avatar BrianPugh commented on July 24, 2024

This is fixed in v2.3.1. If you believe it's not fully fixed we can re-open the issue. Thanks!

from cyclopts.

OrHayat avatar OrHayat commented on July 24, 2024

this is better now but still not fixed completly

this is small simple argparse to show the expected output from list and different input outputs

import argparse

parser=argparse.ArgumentParser()
parser.add_argument("--items",action='append')
parser.add_argument("--stuff",action='store')
if __name__ == "__main__":
    ns=parser.parse_args()
    print(ns)

and some example input output runs

 > ./cli.py --item a --item b --item
usage: cli.py [-h] [--items ITEMS] [--stuff STUFF]
cli.py: error: argument --items: expected one argument

orhayat:~ via 🐹 v1.21.4 via 🐍 v3.10.12
 > ./cli.py --item a --item b --item c
Namespace(items=['a', 'b', 'c'], stuff=None)

orhayat:~ via 🐹 v1.21.4 via 🐍 v3.10.12
 > ./cli.py --item a --item b --item c --stuff=test
Namespace(items=['a', 'b', 'c'], stuff='test')

orhayat:~ via 🐹 v1.21.4 via 🐍 v3.10.12
 > ./cli.py --item a --item b --item=c --stuff=test
Namespace(items=['a', 'b', 'c'], stuff='test')

orhayat:~ via 🐹 v1.21.4 via 🐍 v3.10.12
 > ./cli.py --item a --item b --item= --stuff=test
Namespace(items=['a', 'b', ''], stuff='test')

orhayat:~ via 🐹 v1.21.4 via 🐍 v3.10.12
 > ./cli.py --item a --item b --item= c --stuff=test
usage: cli.py [-h] [--items ITEMS] [--stuff STUFF]
cli.py: error: unrecognized arguments: c

and now small cyclopts command that send list of tuples

import cyclopts
cli=cyclopts.App()

@cli.command
def cmd(items:list[tuple[str,str]]=None,stuff:str=None):
    print(f"items:{items},stuff:{stuff}")

if __name__ == "__main__":
    cli()

version:

pip3 list   |grep cyclopts
cyclopts                   2.3.1
import cyclopts
cli=cyclopts.App()

@cli.command
def cmd(items:list[tuple[str,str]]=None,stuff:str=None):
    print(f"items:{items},stuff:{stuff}")
if __name__ == "__main__":
    cli()
 > ./cli.py cmd --items a b --stuff g
items:[('a', 'b')],stuff:g  # ok

orhayat:~ via 🐹 v1.21.4 via 🐍 v3.10.12
 > ./cli.py cmd --items a  --stuff g
items:[],stuff:g # got single token and skipped it because didnt got other one

orhayat:~ via 🐹 v1.21.4 via 🐍 v3.10.12
 > ./cli.py cmd --items= a  --stuff g
items:[('', 'a')],stuff:g # similar behaviour to argparse 

orhayat:~ via 🐹 v1.21.4 via 🐍 v3.10.12
 > ./cli.py cmd --items= a b --stuff g 
items:[('', 'a')],stuff:g # b token was skipped

from cyclopts.

OrHayat avatar OrHayat commented on July 24, 2024

other example on list of strings


even if u replace the type of list of tuples to list of string it dont behave as expected

```python3
import cyclopts
cli=cyclopts.App()

@cli.command
def cmd(items:list[str]=None,stuff:str=None):
    print(f"items:{items},stuff:{stuff}")
if __name__ == "__main__":
    cli()

./cli.py cmd --items a b
items:['a', 'b'],stuff:None   #ok

orhayat:~ via 🐹 v1.21.4 via 🐍 v3.10.12
 > ./cli.py cmd --items a b --stuff g
items:['a', 'b'],stuff:g #ok


./cli.py cmd --items=a b --stuff g
items:['a', 'b'],stuff:g # ok

./cli.py cmd --items=a # error but should be [a],None....
╭─ Error ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Command "cmd" parameter "--items,--empty-items" requires an argument.                                                                                                 │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯


./cli.py cmd --items= #error  idk what expected (work on argparse but w/e) but anyway look on the next example
╭─ Error ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Command "cmd" parameter "--items,--empty-items" requires an argument.                                                                                                 │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

./cli.py cmd --items= a 
items:['', 'a'],stuff:None 

from cyclopts.

BrianPugh avatar BrianPugh commented on July 24, 2024

thanks for the thorough report! I'll work on this tonight.

from cyclopts.

BrianPugh avatar BrianPugh commented on July 24, 2024

@OrHayat please checkout #109, I believe it should resolve all the issues.

IMHO specifying stuff like --items= a should either

  1. result in ['', 'a']. or:
  2. result in an error saying that you cannot have a space immediately following an =, as this behavior is probably not desirable and could cause confusion.

If you agree with (2), I could add that check.

So similarly, cmd --items= a b --stuff g interprets 3 tokens for --items: '', 'a', 'b', and so it now appropiately raises an exception that the 3 arguments cannot be divisible by two arguments per element.

from cyclopts.

OrHayat avatar OrHayat commented on July 24, 2024

the side branch seems to work as expected.
i think 1 is ok behaviour it the same behaviour as argparse and it also the same behaviour in typer.

from cyclopts.

BrianPugh avatar BrianPugh commented on July 24, 2024

this is fixed in v2.3.2. I think we may revisit the --option= discussion in the future, but I will close this issue for now (but we can certainly re-open if/when you find more bugs!).

from cyclopts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.