Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow adding functions to parser without decorators #29

Closed
neithere opened this issue Aug 20, 2013 · 7 comments
Closed

Allow adding functions to parser without decorators #29

neithere opened this issue Aug 20, 2013 · 7 comments

Comments

@neithere
Copy link
Owner

Current state

Functions cannot be assembled without decorators:

#!python
@command
def simple_one(): pass

@command 
def simple_two(a): pass

@command 
def simple_three(a=1): pass

# function with a merged explicit argument
@arg('-a', '--a', help='foo')
@command 
def simple_four(a=1): pass

# WRONG. this will raise an error
@arg('-x')
@command 
def simple_five(y): pass

Functions that expect a namespace object are considered "normal" despite being a marginal case and looking unpythonic:

#!python
def namespace_one(args): pass

@arg('a')
def namespace_two(args): pass

Desired state

Functions can be assembled without decorators:

#!python
# function with no arguments
def simple_one(): pass

# functions with arguments
def simple_two(a): pass
def simple_three(a=1): pass

# function with a merged explicit argument
@arg('-a', '--a', help='foo')
def simple_four(a=1): pass

# WRONG. this will raise an error
@arg('-x')
def simple_five(y): pass

Functions that expect a namespace object are a special case despite being "classic" from the argparse POV:

#!python
@expects_obj
def namespace_one(args): pass

@arg('a')
@expects_obj
def namespace_two(args): pass

Two possible improvements which I consider "magic" and generally unnecessary and therefore prefer to omit:

  1. detect "classic" command function by signature ("arg" as the first and only argument);
  2. restrict NS-expecting functions (via expects_obj() deco) to only have a single argument called args.

Note: This issue has been automatically migrated from Bitbucket
Created by @neithere on 2012-12-24 08:55:32+00:00, last updated: 2012-12-30 16:34:04+00:00

@ghost ghost assigned neithere Aug 20, 2013
@neithere
Copy link
Owner Author

Additions:

Varargs

#!python
def cmd(*paths):
    pass

# ...is the same as:

@arg('paths', nargs='*')
def cmd(paths):
    pass

# ...and same as:

@arg('paths', nargs='*')
@expects_obj
def cmd(args):
    pass

Keywords

**kwargs contains all arguments not fitting ArgSpec.args and ArgSpec.varargs. If ArgSpec.keywords in None, all @arg()'s will have to fit ArgSpec.args.

#!python

@arg('foo')
@arg('--bar', default=1)
def cmd(**kwargs):
    pass

# ...is the same as:

def cmd(foo, bar=1):
    pass

# ...and same as:

@arg('foo')
@arg('--bar', default=1)
@expects_obj
def cmd(args):
    pass

The first block does not require a default value to be specified.

Note: This comment has been automatically migrated from Bitbucket
Created by @neithere on 2012-12-26 09:15:24+00:00, last updated: 2012-12-26 16:01:27+00:00

@neithere
Copy link
Owner Author

Backwards Compatibility

  1. The command() decorator becomes no-op. It may set an attribute though, see below. A deprecation warning is raised.
  2. If a function is not wrapped in command() and expects a single argument named args, it is likely to follow the old convention, i.e. implicitly expects a namespace object by default. After Argh is upgraded such function will break unless wrapped in expects_obj(). Therefore set_default_function() automatically wraps such function in expects_obj() to ease transition between the versions of API. A deprecation warning is raised.

Release 1.0 removes the command() decorator and the "magical" wrapping of func(args) in expects_obj().

Note: This comment has been automatically migrated from Bitbucket
Created by @neithere on 2012-12-27 07:45:46+00:00

@neithere
Copy link
Owner Author

As DeprecationWarning is no longer loud since Python 2.7, it is required that the application developer runs the code as follows to ensure that the code is up to date:

#!bash
$ python -Wd app.py

Note: This comment has been automatically migrated from Bitbucket
Created by @neithere on 2012-12-27 10:38:48+00:00

@neithere
Copy link
Owner Author

Re issue #29: implement new-style API, deprecate old functions, add compat fixes

Introspection/merging of varargs and keywords is not supported yet.

→ <>

Note: This comment has been automatically migrated from Bitbucket
Created by @neithere on 2012-12-27 16:11:31+00:00

@neithere
Copy link
Owner Author

Re #29: add support for varargs.

→ <<cset 915a6aa05981>>

Note: This comment has been automatically migrated from Bitbucket
Created by @neithere on 2012-12-27 16:18:35+00:00

@neithere
Copy link
Owner Author

Re #29: add support for varkw.

→ <>

Note: This comment has been automatically migrated from Bitbucket
Created by @neithere on 2012-12-27 16:18:35+00:00

@neithere
Copy link
Owner Author

Well, the feature seems to be fully implemented and tested enough. Sure the code needs some refactoring and there are bugs yet to be found, but the issue can be closed.

Note: This comment has been automatically migrated from Bitbucket
Created by @neithere on 2012-12-30 16:34:04+00:00

neithere added a commit that referenced this issue Aug 20, 2013
…ompat fixes

Introspection/merging of varargs and keywords is not supported yet.
neithere added a commit that referenced this issue Aug 20, 2013
neithere added a commit that referenced this issue Aug 20, 2013
saaros added a commit to saaros/argh that referenced this issue Sep 4, 2013
New attributes can't be assigned to instancemethods after they've been
declared so move the old style function detection to the 'alias' decorator
which is usually used in old functions.
saaros added a commit to saaros/argh that referenced this issue Sep 4, 2013
New attributes can't be assigned to instancemethods after they've been
declared so move the old style function detection to the 'alias' decorator
which is usually used in old functions.
saaros added a commit to saaros/argh that referenced this issue Sep 5, 2013
New attributes can't be assigned to instancemethods after they've been
declared so move the old style function detection to the 'arg' decorator.
saaros added a commit to saaros/argh that referenced this issue Sep 5, 2013
New attributes can't be assigned to instancemethods after they've been
declared so move the old style function detection to the 'arg' decorator.
saaros added a commit to saaros/argh that referenced this issue Sep 5, 2013
New attributes can't be assigned to instancemethods after they've been
declared so move the old style function detection to the 'alias' and 'arg'
decorators.
saaros added a commit to saaros/argh that referenced this issue Sep 5, 2013
New attributes can't be assigned to instancemethods after they've been
declared so add old style function detection to the 'alias' and 'arg'
decorators.
neithere added a commit that referenced this issue Jan 5, 2014
compat issue #29: handle old style arguments in objects
hzpc-joostk added a commit to hzpc-joostk/argh that referenced this issue Jan 21, 2020
adds help from varargs annotations

follow up of 3eecce3 and neithere#29
ekimekim pushed a commit to ekimekim/yaargh that referenced this issue Mar 28, 2020
adds help from varargs annotations

follow up of 3eecce3 and neithere#29
ekimekim pushed a commit to ekimekim/yaargh that referenced this issue Mar 28, 2020
adds help from varargs annotations

follow up of 3eecce3 and neithere#29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant