I am trying to use optparse-applicative.
How can I access arguments that are not options?
(From prog --foo --bar=42 baz, I want to get ["baz"])
All the the "high level" functions https://hackage.haskell.org/package/optparse-applicative-0.11.0.2/docs/Options-Applicative-Extra.html
return a where I want (a,[String]).
There is some low-level function https://hackage.haskell.org/package/optparse-applicative-0.11.0.2/docs/Options-Applicative-Common.html#v:runParser but I cannot call it directly because of its type. And indeed I do want to re-use all the plumbing that is in https://hackage.haskell.org/package/optparse-applicative-0.11.0.2/docs/src/Options-Applicative-Extra.html#execParser .
Positional arguments are part of the parser specification. They are not returned separately by the function that runs the parser. The functions argument and strArgument can be used to add a parser for a positional argument to the specification.
Related
Here I have written a function which takes two lists as argument. But when I called this function passing one list as argument, it works well! Why is this working? Here name_function has two arguments but I passed only one list as argument.
def name_function(names=list(),_list=list()):
for name in names:
_list.append(name)
return _list
print(name_function(['mike','smith','bob']))
Here in the function definition you have initialized both the
arguments with the empty list i.e. you are using two default
arguments in the function definition.
For the above reason the function call works if you provide provide
both the arguments or any one of the arguments or no argument at
all.
To learn more about default arguments in python you can refer this
link or this link.
Apparently, if you need to use both keyword and positional arguments while calling your function, you have to use the positional argument first. But the following code results in an error;
def greet(first_name, l_name):
print(f'Hi, {first_name} {last_name}!')
greet('Holmes',
first_name='Harry')
So does it mean that if you're using both, you have to use the positional argument first in the required order, and only then the keyword argument?
Positional arguments must be passed in order as declared in the function. So if you pass three positional arguments, they must go to the first three arguments of the function, and those three arguments can't be passed by keyword. If you want to be able to pass the first argument out of order by keyword, all your arguments must be passed by keyword (or not at all, if they have defaults).
If it helps, Python's binding mechanism is roughly:
Assign positional arguments one by one to sequential parameters of the function. These parameters are now set.
Assign keyword arguments to remaining parameters in any order. If one of the keyword arguments matches an argument already assigned positionally (or the same keyword argument is passed twice), it's an error.
In your case, what this means is that:
greet('Holmes', first_name='Harry')
first binds 'Holmes' to first_name. Then it saw you tried to pass first_name again as a keyword argument and objected.
A haxe function has some parameters whose default values I'd like to use, so I don't need to import anything (they're basic types underneath). If they were last in the parameter order, I could get away with just not including them. But they're first, before some defaults I do want to override.
I'm not allowed to null them on native. _ doesn't compile (I don't think it's meant for this context.) Am I forced to import and copy the defaults in verbatim, or is there another way?
I tried .bind(_, ...)() but that gives Usage of _ is not supported for optional non-nullable arguments.
That error comes from the argument having a non-nullable type (Int, Float or Bool on a static target). If this function is part of your code and not some library, you could just make it nullable with Null<T> or ?.
As long as the arguments are nullable, Haxe also allows you to simply skip them if they are distuingishable (i.e. the type of the value passed must be different from the one(s) you want to skip). This means you don't have to use bind() or explicitly pass null. See the fourth example on the manual's Optional Arguments page.
If making the arguments nullable isn't an option for you in this particular case, you're probably going to have to copy the defaults (although I'm sure it's possible to come up with a clever macro solution for this).
I'm new to Nix and I'm trying to understand the hello derivation given in example.
I can understand the syntax and what is supposed to do, however I don't understand
how the initial arguments (and the especially the perl one_ are fed ?
I mean, who is setting the perl argument before calling this derivation.
Does that mean that perl is a dependency of hello ?
Packages are typically written as set of dependencies -> derivation functions, to be assembled later. The arguments you ask about are fed from pkgs/top-level/all-packages.nix, which holds the set of all packages in Nixpkgs.
When you find the hello's line in all-packages.nix, you'll notice it's using callPackage - it's signature is path to Nix expression -> overrides -> derivation. callPackage loads the path, looks at the function it loaded, and for each arguments provides either value from overrides or, if not given, from the huge set in all-packages.nix.
For a nice description of callPackage see http://lethalman.blogspot.com/2014/09/nix-pill-13-callpackage-design-pattern.html - it's a less condensed explanation, showing how you could have invented callPackage yourself :-).
It seems like it is possible to create optional arguments with argparse that "override" otherwise required arguments (be it positional or required options).
A example would be the --help/-h switch, which just displays the help and exit. Now I need to implement behaviour exactly like this ; I need a switch/option that can be used without using any of the otherwise required arguments.
Take a look at how the 'help' switch is implemented:
self.add_argument(
default_prefix+'h', default_prefix*2+'help',
action='help', default=SUPPRESS,
help=_('show this help message and exit'))
You need to provide an action that short circuits argument processing; the 'help' action does this by exiting the program.