2015-08-02 19:56:03 +00:00
|
|
|
// Copyright (c) 2015 Daniel Grunwald
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
|
|
// software and associated documentation files (the "Software"), to deal in the Software
|
|
|
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
|
|
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
|
|
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all copies or
|
|
|
|
// substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
|
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
|
|
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
|
|
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
//! This module contains logic for parsing a python argument list.
|
2016-03-05 16:41:04 +00:00
|
|
|
//! See also the macros `py_argparse!`, `py_fn!` and `py_method!`.
|
2015-08-02 19:56:03 +00:00
|
|
|
|
|
|
|
use std::ptr;
|
|
|
|
use python::{Python, PythonObject};
|
|
|
|
use objects::{PyObject, PyTuple, PyDict, PyString, exc};
|
|
|
|
use conversion::ToPyObject;
|
|
|
|
use ffi;
|
|
|
|
use err::{self, PyResult};
|
|
|
|
|
2015-08-02 22:06:15 +00:00
|
|
|
/// Description of a python parameter; used for `parse_args()`.
|
2015-08-02 19:56:03 +00:00
|
|
|
pub struct ParamDescription<'a> {
|
2015-08-02 22:06:15 +00:00
|
|
|
/// The name of the parameter.
|
|
|
|
pub name: &'a str,
|
|
|
|
/// Whether the parameter is optional.
|
|
|
|
pub is_optional: bool
|
2015-08-02 19:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse argument list
|
|
|
|
///
|
2015-08-02 22:06:15 +00:00
|
|
|
/// * fname: Name of the current function
|
|
|
|
/// * params: Declared parameters of the function
|
|
|
|
/// * args: Positional arguments
|
|
|
|
/// * kwargs: Keyword arguments
|
|
|
|
/// * output: Output array that receives the arguments.
|
|
|
|
/// Must have same length as `params` and must be initialized to `None`.
|
2015-10-25 16:55:29 +00:00
|
|
|
pub fn parse_args(
|
2015-10-26 22:52:18 +00:00
|
|
|
py: Python,
|
2015-08-02 19:56:03 +00:00
|
|
|
fname: Option<&str>, params: &[ParamDescription],
|
2015-10-25 16:55:29 +00:00
|
|
|
args: &PyTuple, kwargs: Option<&PyDict>,
|
2015-10-26 22:52:18 +00:00
|
|
|
output: &mut[Option<PyObject>]
|
2015-10-25 16:55:29 +00:00
|
|
|
) -> PyResult<()>
|
2015-08-02 19:56:03 +00:00
|
|
|
{
|
|
|
|
assert!(params.len() == output.len());
|
2015-11-07 15:52:20 +00:00
|
|
|
let nargs = args.len(py);
|
2015-10-25 16:55:29 +00:00
|
|
|
let nkeywords = kwargs.map_or(0, |d| d.len(py));
|
2015-08-02 19:56:03 +00:00
|
|
|
if nargs + nkeywords > params.len() {
|
|
|
|
return Err(err::PyErr::new::<exc::TypeError, _>(py,
|
|
|
|
format!("{}{} takes at most {} argument{} ({} given)",
|
|
|
|
fname.unwrap_or("function"),
|
|
|
|
if fname.is_some() { "()" } else { "" },
|
|
|
|
params.len(),
|
|
|
|
if params.len() == 1 { "s" } else { "" },
|
|
|
|
nargs + nkeywords
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
let mut used_keywords = 0;
|
|
|
|
// Iterate through the parameters and assign values to output:
|
|
|
|
for (i, (p, out)) in params.iter().zip(output).enumerate() {
|
2015-10-26 22:52:18 +00:00
|
|
|
match kwargs.and_then(|d| d.get_item(py, p.name)) {
|
2015-08-02 19:56:03 +00:00
|
|
|
Some(kwarg) => {
|
|
|
|
*out = Some(kwarg);
|
|
|
|
used_keywords += 1;
|
|
|
|
if i < nargs {
|
|
|
|
return Err(err::PyErr::new::<exc::TypeError, _>(py,
|
|
|
|
format!("Argument given by name ('{}') and position ({})",
|
|
|
|
p.name, i+1)));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
if i < nargs {
|
2015-10-26 22:52:18 +00:00
|
|
|
*out = Some(args.get_item(py, i));
|
2015-08-02 19:56:03 +00:00
|
|
|
} else {
|
|
|
|
*out = None;
|
|
|
|
if !p.is_optional {
|
|
|
|
return Err(err::PyErr::new::<exc::TypeError, _>(py,
|
|
|
|
format!("Required argument ('{}') (pos {}) not found",
|
|
|
|
p.name, i+1)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if used_keywords != nkeywords {
|
|
|
|
// check for extraneous keyword arguments
|
2015-10-25 16:55:29 +00:00
|
|
|
for (key, _value) in kwargs.unwrap().items(py) {
|
2016-05-05 05:26:52 +00:00
|
|
|
let key = try!(try!(key.cast_as::<PyString>(py)).to_string(py));
|
2015-08-02 19:56:03 +00:00
|
|
|
if !params.iter().any(|p| p.name == key) {
|
|
|
|
return Err(err::PyErr::new::<exc::TypeError, _>(py,
|
|
|
|
format!("'{}' is an invalid keyword argument for this function",
|
|
|
|
key)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2015-08-02 22:06:15 +00:00
|
|
|
/// This macro is used to parse a parameter list into a set of variables.
|
2017-01-23 15:39:42 +00:00
|
|
|
///
|
2015-10-26 22:52:18 +00:00
|
|
|
/// Syntax: `py_argparse!(py, fname, args, kwargs, (parameter-list) { body })`
|
2015-08-02 22:06:15 +00:00
|
|
|
///
|
2015-10-26 22:52:18 +00:00
|
|
|
/// * `py`: the `Python` token
|
2015-08-02 22:06:15 +00:00
|
|
|
/// * `fname`: expression of type `Option<&str>`: Name of the function used in error messages.
|
|
|
|
/// * `args`: expression of type `&PyTuple`: The position arguments
|
|
|
|
/// * `kwargs`: expression of type `Option<&PyDict>`: The named arguments
|
2016-03-07 22:22:44 +00:00
|
|
|
/// * `parameter-list`: a comma-separated list of parameter declarations.
|
|
|
|
/// Parameter declarations have one of these formats:
|
|
|
|
/// 1. `name`
|
|
|
|
/// 2. `name: ty`
|
|
|
|
/// 3. `name: ty = default_value`
|
|
|
|
/// 4. `*name`
|
|
|
|
/// 5. `*name : ty`
|
|
|
|
/// 6. `**name`
|
|
|
|
/// 7. `**name : ty`
|
2017-01-27 20:51:56 +00:00
|
|
|
///
|
2016-07-22 08:04:25 +00:00
|
|
|
/// The types used must implement the `FromPyObject` trait.
|
2016-03-07 22:22:44 +00:00
|
|
|
/// If no type is specified, the parameter implicitly uses
|
|
|
|
/// `&PyObject` (format 1), `&PyTuple` (format 4) or `&PyDict` (format 6).
|
|
|
|
/// If a default value is specified, it must be a compile-time constant
|
|
|
|
// of type `ty`.
|
2015-08-02 22:06:15 +00:00
|
|
|
/// * `body`: expression of type `PyResult<_>`.
|
2016-03-07 22:22:44 +00:00
|
|
|
/// The extracted argument values are available in this scope.
|
2015-08-02 22:06:15 +00:00
|
|
|
///
|
|
|
|
/// `py_argparse!()` expands to code that extracts values from `args` and `kwargs` and assigns
|
|
|
|
/// them to the parameters. If the extraction is successful, `py_argparse!()` evaluates
|
2016-03-07 22:22:44 +00:00
|
|
|
/// the body expression and returns of that evaluation.
|
2015-08-02 22:06:15 +00:00
|
|
|
/// If extraction fails, `py_argparse!()` returns a failed `PyResult` without evaluating `body`.
|
2017-01-27 20:51:56 +00:00
|
|
|
///
|
|
|
|
/// The `py_argparse!()` macro special-cases reference types (when `ty` starts with a `&` token):
|
|
|
|
/// In this case, the macro uses the `RefFromPyObject` trait instead of the `FromPyObject` trait.
|
|
|
|
/// When using at least one reference parameter, the `body` block is placed within a closure,
|
|
|
|
/// so `return` statements might behave unexpectedly in this case. (this only affects direct use
|
|
|
|
/// of `py_argparse!`; `py_fn!` is unaffected as the body there is always in a separate function
|
|
|
|
/// from the generated argument-parsing code).
|
2015-08-02 22:06:15 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! py_argparse {
|
2016-03-07 22:22:44 +00:00
|
|
|
($py:expr, $fname:expr, $args:expr, $kwargs:expr, $plist:tt $body:block) => {
|
|
|
|
py_argparse_parse_plist! { py_argparse_impl { $py, $fname, $args, $kwargs, $body, } $plist }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
#[doc(hidden)]
|
|
|
|
macro_rules! py_argparse_parse_plist {
|
|
|
|
// Parses a parameter-list into a format more suitable for consumption by Rust macros.
|
|
|
|
// py_argparse_parse_plist! { callback { initial_args } (plist) }
|
2016-03-07 22:48:44 +00:00
|
|
|
// = callback! { initial_args [{ pname:ptype = [ {**} {default-value} ] } ...] }
|
2016-03-07 22:22:44 +00:00
|
|
|
// The braces around the *s and the default-value are used even if they are empty.
|
|
|
|
|
|
|
|
// Special-case entry-point for empty parameter list:
|
|
|
|
{ $callback:ident { $($initial_arg:tt)* } ( ) } => {
|
|
|
|
$callback! { $($initial_arg)* [] }
|
|
|
|
};
|
|
|
|
// Regular entry point for non-empty parameter list:
|
|
|
|
{ $callback:ident $initial_args:tt ( $( $p:tt )+ ) } => {
|
|
|
|
// add trailing comma to plist so that the parsing step can assume every
|
|
|
|
// parameter ends with a comma.
|
|
|
|
py_argparse_parse_plist_impl! { $callback $initial_args [] ( $($p)*, ) }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
#[doc(hidden)]
|
|
|
|
macro_rules! py_argparse_parse_plist_impl {
|
|
|
|
// TT muncher macro that does the main work for py_argparse_parse_plist!.
|
|
|
|
|
|
|
|
// Base case: all parameters handled
|
|
|
|
{ $callback:ident { $($initial_arg:tt)* } $output:tt ( ) } => {
|
|
|
|
$callback! { $($initial_arg)* $output }
|
|
|
|
};
|
2016-05-08 19:25:09 +00:00
|
|
|
// Kwargs parameter with reference extraction
|
|
|
|
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
|
|
|
|
( ** $name:ident : &$t:ty , $($tail:tt)* )
|
|
|
|
} => {
|
|
|
|
py_argparse_parse_plist_impl! {
|
|
|
|
$callback $initial_args
|
|
|
|
[ $($output)* { $name:&$t = [ {**} {} {$t} ] } ]
|
|
|
|
($($tail)*)
|
|
|
|
}
|
|
|
|
};
|
2016-03-07 22:22:44 +00:00
|
|
|
// Kwargs parameter
|
|
|
|
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
|
|
|
|
( ** $name:ident : $t:ty , $($tail:tt)* )
|
|
|
|
} => {
|
|
|
|
py_argparse_parse_plist_impl! {
|
|
|
|
$callback $initial_args
|
2016-05-08 19:25:09 +00:00
|
|
|
[ $($output)* { $name:$t = [ {**} {} {} ] } ]
|
2016-03-07 22:22:44 +00:00
|
|
|
($($tail)*)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Kwargs parameter with implicit type
|
|
|
|
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
|
|
|
|
( ** $name:ident , $($tail:tt)* )
|
|
|
|
} => {
|
|
|
|
py_argparse_parse_plist_impl! {
|
|
|
|
$callback $initial_args
|
2016-05-08 19:25:09 +00:00
|
|
|
[ $($output)* { $name:Option<&$crate::PyDict> = [ {**} {} {} ] } ]
|
|
|
|
($($tail)*)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Varargs parameter with reference extraction
|
|
|
|
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
|
|
|
|
( * $name:ident : &$t:ty , $($tail:tt)* )
|
|
|
|
} => {
|
|
|
|
py_argparse_parse_plist_impl! {
|
|
|
|
$callback $initial_args
|
|
|
|
[ $($output)* { $name:&$t = [ {*} {} {$t} ] } ]
|
2016-03-07 22:22:44 +00:00
|
|
|
($($tail)*)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Varargs parameter
|
|
|
|
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
|
|
|
|
( * $name:ident : $t:ty , $($tail:tt)* )
|
|
|
|
} => {
|
|
|
|
py_argparse_parse_plist_impl! {
|
|
|
|
$callback $initial_args
|
2016-05-08 19:25:09 +00:00
|
|
|
[ $($output)* { $name:$t = [ {*} {} {} ] } ]
|
2016-03-07 22:22:44 +00:00
|
|
|
($($tail)*)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Varargs parameter with implicit type
|
|
|
|
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
|
|
|
|
( * $name:ident , $($tail:tt)* )
|
|
|
|
} => {
|
|
|
|
py_argparse_parse_plist_impl! {
|
|
|
|
$callback $initial_args
|
2016-05-08 19:25:09 +00:00
|
|
|
[ $($output)* { $name:&$crate::PyTuple = [ {*} {} {} ] } ]
|
|
|
|
($($tail)*)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Simple parameter with reference extraction
|
|
|
|
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
|
|
|
|
( $name:ident : &$t:ty , $($tail:tt)* )
|
|
|
|
} => {
|
|
|
|
py_argparse_parse_plist_impl! {
|
|
|
|
$callback $initial_args
|
|
|
|
[ $($output)* { $name:&$t = [ {} {} {$t} ] } ]
|
2016-03-07 22:22:44 +00:00
|
|
|
($($tail)*)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Simple parameter
|
|
|
|
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
|
|
|
|
( $name:ident : $t:ty , $($tail:tt)* )
|
|
|
|
} => {
|
|
|
|
py_argparse_parse_plist_impl! {
|
|
|
|
$callback $initial_args
|
2016-05-08 19:25:09 +00:00
|
|
|
[ $($output)* { $name:$t = [ {} {} {} ] } ]
|
2016-03-07 22:22:44 +00:00
|
|
|
($($tail)*)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Simple parameter with implicit type
|
|
|
|
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
|
|
|
|
( $name:ident , $($tail:tt)* )
|
|
|
|
} => {
|
|
|
|
py_argparse_parse_plist_impl! {
|
|
|
|
$callback $initial_args
|
2016-05-08 19:25:09 +00:00
|
|
|
[ $($output)* { $name:&$crate::PyObject = [ {} {} {} ] } ]
|
2016-03-07 22:22:44 +00:00
|
|
|
($($tail)*)
|
|
|
|
}
|
|
|
|
};
|
2017-01-26 20:35:16 +00:00
|
|
|
// Optional parameter with reference extraction
|
|
|
|
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
|
|
|
|
( $name:ident : &$t:ty = $default:expr, $($tail:tt)* )
|
|
|
|
} => {
|
|
|
|
py_argparse_parse_plist_impl! {
|
|
|
|
$callback $initial_args
|
|
|
|
[ $($output)* { $name:&$t = [ {} {$default} {$t} ] } ]
|
|
|
|
($($tail)*)
|
|
|
|
}
|
|
|
|
};
|
2016-03-07 22:22:44 +00:00
|
|
|
// Optional parameter
|
|
|
|
{ $callback:ident $initial_args:tt [ $($output:tt)* ]
|
|
|
|
( $name:ident : $t:ty = $default:expr , $($tail:tt)* )
|
|
|
|
} => {
|
|
|
|
py_argparse_parse_plist_impl! {
|
|
|
|
$callback $initial_args
|
2016-05-08 19:25:09 +00:00
|
|
|
[ $($output)* { $name:$t = [ {} {$default} {} ] } ]
|
2016-03-07 22:22:44 +00:00
|
|
|
($($tail)*)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// The main py_argparse!() macro, except that it expects the parameter-list
|
|
|
|
// in the output format of py_argparse_parse_plist!().
|
|
|
|
#[macro_export]
|
|
|
|
#[doc(hidden)]
|
|
|
|
macro_rules! py_argparse_impl {
|
2016-03-12 17:14:21 +00:00
|
|
|
// special case: function signature is (*args, **kwargs),
|
|
|
|
// so we can directly pass along our inputs without calling parse_args().
|
|
|
|
($py:expr, $fname:expr, $args:expr, $kwargs:expr, $body:block,
|
|
|
|
[
|
2016-05-08 19:25:09 +00:00
|
|
|
{ $pargs:ident : $pargs_type:ty = [ {*} {} {} ] }
|
|
|
|
{ $pkwargs:ident : $pkwargs_type:ty = [ {**} {} {} ] }
|
2016-03-12 17:14:21 +00:00
|
|
|
]
|
|
|
|
) => {{
|
2016-05-08 19:25:09 +00:00
|
|
|
let _py: $crate::Python = $py;
|
|
|
|
// TODO: use extract() to be more flexible in which type is expected
|
2016-03-12 17:14:21 +00:00
|
|
|
let $pargs: $pargs_type = $args;
|
2016-05-08 19:25:09 +00:00
|
|
|
let $pkwargs: $pkwargs_type = $kwargs;
|
|
|
|
$body
|
2016-03-12 17:14:21 +00:00
|
|
|
}};
|
|
|
|
|
|
|
|
// normal argparse logic
|
2016-03-07 22:22:44 +00:00
|
|
|
($py:expr, $fname:expr, $args:expr, $kwargs:expr, $body:block,
|
2016-03-07 22:48:44 +00:00
|
|
|
[ $( { $pname:ident : $ptype:ty = $detail:tt } )* ]
|
2016-03-07 22:22:44 +00:00
|
|
|
) => {{
|
2015-08-02 19:56:03 +00:00
|
|
|
const PARAMS: &'static [$crate::argparse::ParamDescription<'static>] = &[
|
|
|
|
$(
|
2016-03-07 22:48:44 +00:00
|
|
|
py_argparse_param_description! { $pname : $ptype = $detail }
|
2015-08-02 19:56:03 +00:00
|
|
|
),*
|
|
|
|
];
|
2015-10-25 16:55:29 +00:00
|
|
|
let py: $crate::Python = $py;
|
2016-03-07 22:22:44 +00:00
|
|
|
let mut output = [$( py_replace_expr!($pname None) ),*];
|
2015-10-26 22:52:18 +00:00
|
|
|
match $crate::argparse::parse_args(py, $fname, PARAMS, $args, $kwargs, &mut output) {
|
2015-08-02 19:56:03 +00:00
|
|
|
Ok(()) => {
|
2016-03-07 22:22:44 +00:00
|
|
|
// Experimental slice pattern syntax would be really nice here (#23121)
|
|
|
|
//let [$(ref $pname),*] = output;
|
|
|
|
// We'll use an iterator instead.
|
|
|
|
let mut _iter = output.iter();
|
|
|
|
// We'll have to generate a bunch of nested `match` statements
|
|
|
|
// (at least until we can use ? + catch, assuming that will be hygienic wrt. macros),
|
|
|
|
// so use a recursive helper macro for that:
|
|
|
|
py_argparse_extract!( py, _iter, $body,
|
2016-03-07 22:48:44 +00:00
|
|
|
[ $( { $pname : $ptype = $detail } )* ])
|
2015-08-02 19:56:03 +00:00
|
|
|
},
|
|
|
|
Err(e) => Err(e)
|
|
|
|
}
|
2016-03-06 12:33:57 +00:00
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2016-03-07 22:22:44 +00:00
|
|
|
// Like py_argparse_impl!(), but accepts `*mut ffi::PyObject` for $args and $kwargs.
|
|
|
|
#[macro_export]
|
|
|
|
#[doc(hidden)]
|
|
|
|
macro_rules! py_argparse_raw {
|
|
|
|
($py:ident, $fname:expr, $args:expr, $kwargs:expr, $plist:tt $body:block) => {{
|
|
|
|
let args: $crate::PyTuple = $crate::PyObject::from_borrowed_ptr($py, $args).unchecked_cast_into();
|
|
|
|
let kwargs: Option<$crate::PyDict> = $crate::argparse::get_kwargs($py, $kwargs);
|
|
|
|
let ret = py_argparse_impl!($py, $fname, &args, kwargs.as_ref(), $body, $plist);
|
|
|
|
$crate::PyDrop::release_ref(args, $py);
|
|
|
|
$crate::PyDrop::release_ref(kwargs, $py);
|
|
|
|
ret
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub unsafe fn get_kwargs(py: Python, ptr: *mut ffi::PyObject) -> Option<PyDict> {
|
|
|
|
if ptr.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(PyObject::from_borrowed_ptr(py, ptr).unchecked_cast_into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
#[doc(hidden)]
|
|
|
|
macro_rules! py_argparse_param_description {
|
|
|
|
// normal parameter
|
2016-05-08 19:25:09 +00:00
|
|
|
{ $pname:ident : $ptype:ty = [ {} {} $rtype:tt ] } => (
|
2016-03-07 22:22:44 +00:00
|
|
|
$crate::argparse::ParamDescription {
|
|
|
|
name: stringify!($pname),
|
|
|
|
is_optional: false
|
|
|
|
}
|
|
|
|
);
|
2017-01-26 20:35:16 +00:00
|
|
|
// optional parameters
|
|
|
|
{ $pname:ident : $ptype:ty = [ {} {$default:expr} {$($rtype:tt)*} ] } => (
|
2017-01-23 15:39:42 +00:00
|
|
|
$crate::argparse::ParamDescription {
|
|
|
|
name: stringify!($pname),
|
|
|
|
is_optional: true
|
|
|
|
}
|
|
|
|
);
|
2016-03-07 22:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
#[doc(hidden)]
|
|
|
|
macro_rules! py_argparse_extract {
|
|
|
|
// base case
|
|
|
|
( $py:expr, $iter:expr, $body:block, [] ) => { $body };
|
|
|
|
// normal parameter
|
|
|
|
( $py:expr, $iter:expr, $body:block,
|
2016-05-08 19:25:09 +00:00
|
|
|
[ { $pname:ident : $ptype:ty = [ {} {} {} ] } $($tail:tt)* ]
|
2016-03-07 22:22:44 +00:00
|
|
|
) => {
|
|
|
|
// First unwrap() asserts the iterated sequence is long enough (which should be guaranteed);
|
|
|
|
// second unwrap() asserts the parameter was not missing (which fn parse_args already checked for).
|
2016-05-08 19:25:09 +00:00
|
|
|
match <$ptype as $crate::FromPyObject>::extract($py, $iter.next().unwrap().as_ref().unwrap()) {
|
|
|
|
Ok($pname) => py_argparse_extract!($py, $iter, $body, [$($tail)*]),
|
2016-03-07 22:22:44 +00:00
|
|
|
Err(e) => Err(e)
|
|
|
|
}
|
|
|
|
};
|
2016-05-08 19:25:09 +00:00
|
|
|
// normal parameter with reference extraction
|
|
|
|
( $py:expr, $iter:expr, $body:block,
|
|
|
|
[ { $pname:ident : $ptype:ty = [ {} {} {$rtype:ty} ] } $($tail:tt)* ]
|
|
|
|
) => {
|
|
|
|
// First unwrap() asserts the iterated sequence is long enough (which should be guaranteed);
|
|
|
|
// second unwrap() asserts the parameter was not missing (which fn parse_args already checked for).
|
|
|
|
match <$rtype as $crate::RefFromPyObject>::with_extracted($py,
|
|
|
|
$iter.next().unwrap().as_ref().unwrap(),
|
|
|
|
|$pname: $ptype| py_argparse_extract!($py, $iter, $body, [$($tail)*])
|
|
|
|
) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => Err(e)
|
2016-03-06 12:33:57 +00:00
|
|
|
}
|
|
|
|
};
|
2017-01-23 15:39:42 +00:00
|
|
|
// optional parameter
|
|
|
|
( $py:expr, $iter:expr, $body:block,
|
|
|
|
[ { $pname:ident : $ptype:ty = [ {} {$default:expr} {} ] } $($tail:tt)* ]
|
|
|
|
) => {
|
|
|
|
match $iter.next().unwrap().as_ref().map(|obj| obj.extract::<_>($py)).unwrap_or(Ok($default)) {
|
|
|
|
Ok($pname) => py_argparse_extract!($py, $iter, $body, [$($tail)*]),
|
|
|
|
Err(e) => Err(e)
|
|
|
|
}
|
|
|
|
};
|
2017-01-26 20:35:16 +00:00
|
|
|
// optional parameter with reference extraction
|
|
|
|
( $py:expr, $iter:expr, $body:block,
|
|
|
|
[ { $pname:ident : $ptype:ty = [ {} {$default:expr} {$rtype:ty} ] } $($tail:tt)* ]
|
|
|
|
) => {
|
|
|
|
//unwrap() asserts the iterated sequence is long enough (which should be guaranteed);
|
|
|
|
match <$rtype as $crate::RefFromPyObject>::with_extracted($py,
|
|
|
|
$iter.next().unwrap().as_ref().unwrap_or($default.into_py_object($py).as_object()),
|
|
|
|
|$pname: $ptype| py_argparse_extract!($py, $iter, $body, [$($tail)*])
|
|
|
|
) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => Err(e)
|
|
|
|
}
|
|
|
|
};
|
2016-03-06 12:33:57 +00:00
|
|
|
}
|
|
|
|
|
2015-08-02 19:56:03 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use python::{Python, PythonObject};
|
2017-01-23 15:39:42 +00:00
|
|
|
use objects::PyTuple;
|
2015-08-02 19:56:03 +00:00
|
|
|
use conversion::ToPyObject;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_parse() {
|
|
|
|
let gil_guard = Python::acquire_gil();
|
|
|
|
let py = gil_guard.python();
|
|
|
|
let mut called = false;
|
|
|
|
let tuple = ("abc", 42).to_py_object(py);
|
2015-10-26 22:52:18 +00:00
|
|
|
py_argparse!(py, None, &tuple, None, (x: &str, y: i32) {
|
2015-08-02 19:56:03 +00:00
|
|
|
assert_eq!(x, "abc");
|
|
|
|
assert_eq!(y, 42);
|
|
|
|
called = true;
|
|
|
|
Ok(())
|
|
|
|
}).unwrap();
|
|
|
|
assert!(called);
|
|
|
|
}
|
2016-05-08 19:25:09 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_default_param_type() {
|
|
|
|
let gil_guard = Python::acquire_gil();
|
|
|
|
let py = gil_guard.python();
|
|
|
|
let mut called = false;
|
|
|
|
let tuple = ("abc",).to_py_object(py);
|
|
|
|
py_argparse!(py, None, &tuple, None, (x) {
|
|
|
|
assert_eq!(*x, tuple.get_item(py, 0));
|
|
|
|
called = true;
|
|
|
|
Ok(())
|
|
|
|
}).unwrap();
|
|
|
|
assert!(called);
|
|
|
|
}
|
2015-08-02 19:56:03 +00:00
|
|
|
|
2017-01-23 15:39:42 +00:00
|
|
|
#[test]
|
|
|
|
pub fn test_default_value() {
|
|
|
|
let gil_guard = Python::acquire_gil();
|
|
|
|
let py = gil_guard.python();
|
|
|
|
let mut called = false;
|
2017-01-26 20:35:16 +00:00
|
|
|
let tuple = (0, "foo").to_py_object(py);
|
|
|
|
py_argparse!(py, None, &tuple, None, (x: usize = 42, y: &str = "abc") {
|
2017-01-23 15:39:42 +00:00
|
|
|
assert_eq!(x, 0);
|
2017-01-26 20:35:16 +00:00
|
|
|
assert_eq!(y, "foo");
|
2017-01-23 15:39:42 +00:00
|
|
|
called = true;
|
|
|
|
Ok(())
|
|
|
|
}).unwrap();
|
|
|
|
assert!(called);
|
|
|
|
|
|
|
|
let mut called = false;
|
|
|
|
let tuple = PyTuple::new(py, &[]);
|
2017-01-26 20:35:16 +00:00
|
|
|
py_argparse!(py, None, &tuple, None, (x: usize = 42, y: &str = "abc") {
|
2017-01-23 15:39:42 +00:00
|
|
|
assert_eq!(x, 42);
|
2017-01-26 20:35:16 +00:00
|
|
|
assert_eq!(y, "abc");
|
2017-01-23 15:39:42 +00:00
|
|
|
called = true;
|
|
|
|
Ok(())
|
|
|
|
}).unwrap();
|
|
|
|
assert!(called);
|
|
|
|
}
|
|
|
|
}
|