Hi all,
I have a situation, maybe someone can give some insight.
Say I want to have input which is comma separated array (e.g. paths='path1,path2,path3') and convert it to the desired output - list:
Now the second case. I want input to be space separated array - bash array. And I want space-separated string returned. My current approach is:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('paths', nargs='+')
args = parser.parse_args()
paths = ' '.join(args.paths)
But what I am looking for is a way to do this, which is intrinsic to `argparse` module. Reason being I have a fair amount of such cases and I don’t want to do post-processing, where post-post-processing happens (after `parser.parse_args()`).
Hi all,
I have a situation, maybe someone can give some insight.
Say I want to have input which is comma separated array (e.g. paths='path1,path2,path3') and convert it to the desired output - list:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('paths', type=lambda x: list(filter(str.strip, x.split(','))))
So far so good. But this is just an example of what sort of solution I am after.
Now the second case. I want input to be space separated array - bash array. And I want space-separated string returned. My current approach is:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('paths', nargs='+')
args = parser.parse_args()
paths = ' '.join(args.paths)
But what I am looking for is a way to do this, which is intrinsic to `argparse` module. Reason being I have a fair amount of such cases and I don’t want to do post-processing, where post-post-processing happens (after `parser.parse_args()`).
I have tried overloading `parse_args` with post-processor arguments, and that seemed fine, but it stopped working when I had sub-parsers, which are defined in different modules and do not call `parse_args` themselves.
Thank you, exactly what I was looking for!
One more question following this. Is there a way to have a customisable action? I.e. What if I want to join with space in one case and with coma in another. Is there a way to reuse the same action class?
On 27 Nov 2023, at 22:36, Mats Wichmann <mats@wichmann.us> wrote:significant :-; so I'm not really an expert.
On 11/27/23 13:21, Dom Grigonis wrote:
Thank you, exactly what I was looking for!
One more question following this. Is there a way to have a customisable action? I.e. What if I want to join with space in one case and with coma in another. Is there a way to reuse the same action class?
I've worked more with optparse (the project I work on that uses it has reasons why it's not feasible to convert to argparse); in optparse you use a callback function, rather than an action class, and the change to a callable class is somewhat
The question is how you determine which you want to do - then there's no problem for the action class's call method to implement it. I presume you can write an initializer class that takes an extra argument, collect that and stuff it into an instancevariable, then use super to call the base Action class's initializer with the rest of the args
super().__init__(option_strings=option_strings, *args, **kwargs)
Hopefully someone else has done this kind of thing because now I'm just guessing!
On 27 Nov 2023, at 21:55, Mats Wichmann via Python-list <python-list@python.org> wrote:
On 11/27/23 04:29, Dom Grigonis via Python-list wrote:
Hi all,
I have a situation, maybe someone can give some insight.
Say I want to have input which is comma separated array (e.g. paths='path1,path2,path3') and convert it to the desired output - list:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('paths', type=lambda x: list(filter(str.strip, x.split(','))))
So far so good. But this is just an example of what sort of solution I am after.
Maybe use "action" rather than "type" here? the conversion of a csv argument into words seems more like an action.
Now the second case. I want input to be space separated array - bash array. And I want space-separated string returned. My current approach is:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('paths', nargs='+')
args = parser.parse_args()
paths = ' '.join(args.paths)
But what I am looking for is a way to do this, which is intrinsic to `argparse` module. Reason being I have a fair amount of such cases and I don’t want to do post-processing, where post-post-processing happens (after `parser.parse_args()`).
I have tried overloading `parse_args` with post-processor arguments, and that seemed fine, but it stopped working when I had sub-parsers, which are defined in different modules and do not call `parse_args` themselves.
Depending on what *else* you need to handle it may or not may work here to just collect these from the remainders, and then use an action to join them, like:
import argparse
class JoinAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, ' '.join(values))
parser = argparse.ArgumentParser()
parser.add_argument('paths', nargs=argparse.REMAINDER, action=JoinAction) args = parser.parse_args()
print(f"{args.paths!r}")
--
https://mail.python.org/mailman/listinfo/python-list
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 04:42:42 |
Calls: | 10,386 |
Calls today: | 1 |
Files: | 14,058 |
Messages: | 6,416,625 |