How do I ignore escape characters in command line arguments? - command-line

Consider a simple F# console application that echoes the command line arguments passed to it, one command per line:
[<EntryPoint>]
let main argv =
argv |> Array.iter (printfn "%s")
0
If I pass this program a directory path with a trailing slash, it treats the slash as an escape character producing unexpected results:
> app.exe "c:\some path\" "c:\another path"
c:\some path" c:\another
path
How can I retrieve the command line arguments without interpreting a slash as an escape character?

System.Environment.CommandLine gives you access to the complete unescaped command line, and you are free to process that however you like.
But unless you plan to implement a full-blown parser in your app, this is not going to be a reliable approach, and it's unlikely you will be able to reliably handle all of the byzantine escaping rules implemented by all of the various shells your app might have been invoked in. e.g. cmd.exe escapes things differently from powershell.exe, which is in turn different from Cygwin, etc etc.
A better choice is to simply take argv as-is, validate it as you see fit (e.g. File.Exists(argv.[0]) or whatever), and bail out with a helpful error message if the user messed something up.

Related

Is there a way to add a command to neovim that can be called as `:command`?

What I would like to archieve is to be able to use :< shell command and add the output to the shell command to the cursor's position in neovim.
It seems I could use :redir to archieve this functionality and wrap it in to a function.
Is there a way to associate a [neo]vim function to :command?
A command can't return value. Hence a command is not an expression. And in particular, commands never can be "nested" one into another, like function calls. Cf. Shell scripting, for example.
In principle, there are some tricks, like a builtin function that accepts command name, runs it, and returns output as string value. But this is rather "convenient redir", not "syntax breaker".
To add external tool output into cursor position use, for example,
put=system('blah blah')
This is legal, as (some of) commands may accept expressions, including function calls.
Make sure to escape "bars" and "double quotes" though, as (sometimes) they are special in VimScript.

passing exclamation, brackets as part of executable arguments in powershell

In git, if we want to exclude a file from diff output (on bash) we do something like follows:
git diff -- !(file.txt)
But this is disallowed in powershell. Hence is there a way to achieve this in the powershell prompt?
Simply single-quote your argument:
git diff -- '!(file.txt)'
Single-quoting makes PowerShell treat the string literally and prevents it from interpreting chars. such as ( as its own metacharacters.
Before invoking the target program, PowerShell re-quotes arguments if and as needed, behind the scenes; that is:
It encloses an argument in "..." if it contains whitespace, and also in certain, less common scenarios (see link below).
It passes it without quotes otherwise.
Note: There are pitfalls associated with this invisible re-quoting - see this answer.

Separating interpreter command line arguments from script arguments

Suppose you have an interpreter that takes command line arguments including the filenames for a script to be run, and optionally further arguments to be passed to the script. The interpreter then needs to figure out which of the given arguments were intended for its own use and which were intended to be passed to the script.
Is there a widely used convention for separating the former from the latter?
With Bash
-- A -- signals the end of options and disables further option
processing. Any arguments after the -- are treated as file-
names and arguments. An argument of - is equivalent to --.
With CMD :: is often used as a dummy argument to indicate recusrsion for instance. Prefixed on a line, it can be used to turn the line into a comment, giving a handy "programmed-skip" facility

Possible to use Powershell to type a command into a third-party command line program?

I have an old, third party, command line, proprietary program which I'm calling from PowerShell.
Once started, this program accepts commands typed in followed by enter (like any other program), but it's very basic. It doesn't have flags, doesn't accept piped in arguments, etc. You have to start the program, type your command, hit enter and parse the results.
Is there a way I can use PowerShell to type in a command and get the resulting output? Right now the best solution I have is to call SendKeys.Send in a background job, but I'm not sure this will work.
Is there a better way?
check out this to see if it would work for you: http://wasp.codeplex.com/
legacy programs are hard to tell, however. this works with standard windows programs.

How does Getopt::Std handle spaces in arguments on the command line?

I've been playing around with the Getopt::Std module and was wondering about arguments taking spaces.
I have this code atm: getopts('dp:h', \%options);
The problem is, that if the argument following the p flag contains a space, getopts stops processing the list right when it hits the space. Is there a way I can allow spaces in the arguments without having to wrap the arguments following the flag in quotes (-p "something something")?
I'm fine with quotes. I'm just curious. Thanks guys!
Take a look here, someone did a lot of experimenting ..
and this page indicates that Double Quotes " char(34) will work, if you have spaces embedded, thus implying that there is no other way ..