‏إظهار الرسائل ذات التسميات positional. إظهار كافة الرسائل
‏إظهار الرسائل ذات التسميات positional. إظهار كافة الرسائل

الأربعاء، 25 ديسمبر 2013

argparse optional positional argument and subparsers arguments

To argp the subparser argument looks just like another positional, one that takes choices (the names of the subparsers). Also argp knows nothing about pos_arg1. That's in tmpp's list of arguments.

When argp sees filename command1 otherarg, filename and command1 satisfy its 2 positionals. otherarg is then passed on the tmpp.

With command1 otherarg, again 2 strings, 2 argp positionals. command is assigned to inputfile. There's no logic to backtrack and say command1 fits subcommands better, or that `tmpp' needs one of those strings.

You could change the 1st positional to an optional, --inputfile.

Or you could inputfile another positional of tmpp. If a number of the subparsers need it it, consider using parents.

argparse isn't a smart as you, and can't 'think ahead' or 'backtrack'. If it appears to do something smart it's because it uses re pattern matching to handle nargs values (e.g. ?, *, +).

EDIT

One way to 'trick' argparse into recognizing the first positional as the subparser is to insert an optional after it. With command1 -b xxx otherarg, -b xxx breaks up the list of positional strings, so only command1 is matched against inputfile and subcommands.

p=argparse.ArgumentParser()p.add_argument('file',nargs='?',default='foo')sp = p.add_subparsers(dest='cmd')spp = sp.add_parser('cmd1')spp.add_argument('subfile')spp.add_argument('-b')p.parse_args('cmd1 -b x three'.split())# Namespace(b='x', cmd='cmd1', file='foo', subfile='three')

While argparse allows variable length positionals in any order, how it handles them can be confusing. It's easier to predict what argparse will do if there is only one such positional, and it occurs at the end.


View the original article here

الثلاثاء، 24 ديسمبر 2013

Understanding Cmdlet positional parameter

All, Forgive me I am a newbie for the Power Shell, Currently I was reading book Windows PowerShell CookBook try to get start with it. So far, Everything make sense to me except one thing, I was totally confused with the Positional Parameter to Cmdlet.

For example: Select-String

The syntax is following :

Select-String [-Pattern] [-Path] [-AllMatches] [-CaseSensitive] [-Context ][-Encoding ] [-Exclude ] [-Include ] [-List] [-NotMatch] [-Quiet] [-SimpleMatch][]Select-String [-Pattern] [-AllMatches] [-CaseSensitive] [-Context ] [-Encoding ][-Exclude ] [-Include ] [-List] [-NotMatch] [-Quiet] [-SimpleMatch] -InputObject []Select-String [-Pattern] [-AllMatches] [-CaseSensitive] [-Context ] [-Encoding ][-Exclude ] [-Include ] [-List] [-NotMatch] [-Quiet] [-SimpleMatch] -LiteralPath []

I can pass the parameter value to the Cmdlet directly by ignoring the parameter name . like below:

"Hello World"|Select-String .

Based on the concept of Positional parameter,Because the parameter -Pattern is the first parameter. the value . can match the parameter -Pattern. It is ok for me to understand.

But When I try this command line "hello world" | Select-String -AllMatches . the value . is not in the first. Why the Select-String can know it and work out the result ? Could someone please tell me more to better understand it ? thanks.


View the original article here