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
Related
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.
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.
The question is described on the title, basically I'd like to execute a command line from scheme, let's say 'ls' and obtaining the output. So my questions are:
Is it possible?
How?
Thanks a lot in advance!
By the way I use Guille.
You need one of these system and system*.
Example: (system "ls")
From the documentation: Guile Reference
— Scheme Procedure: system [cmd]
— C Function: scm_system (cmd)
Execute cmd using the operating system's “command processor”. Under Unix this is usually the default shell sh. The value returned is cmd's exit status as returned by waitpid, which can be interpreted using the functions above.
If system is called without arguments, return a boolean indicating whether the command processor is available.
— Scheme Procedure: system* . args
— C Function: scm_system_star (args)
Execute the command indicated by args. The first element must be a string indicating the command to be executed, and the remaining items must be strings representing each of the arguments to that command.
This function returns the exit status of the command as provided by waitpid. This value can be handled with status:exit-val and the related functions.
system* is similar to system, but accepts only one string per-argument, and performs no shell interpretation. The command is executed using fork and execlp. Accordingly this function may be safer than system in situations where shell interpretation is not required.
Example: (system* "echo" "foo" "bar")
I wish to do
lua prog.lua arg1 arg2
from the command line
Inside prog.lua, I want to say, for instance
print (arg1, arg2, '\n')
Lua doesn't seem to have argv[1] etc and the methods I've seen for dealing with command line arguments seem to be immature and / or cumbersome. Am I missing something?
You're missing the arg vector, which has the elements you want in arg[1], arg[2], and so on:
% lua -i -- /dev/null one two three
Lua 5.1.3 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> print(arg[2])
two
>
More info in the Lua manual section on Lua standalone (thanks Miles!).
In addition to the arg table, ... contains the arguments (arg[1] and up) used to invoke the script.
% lua -i -- /dev/null one two three
Lua 5.1.3 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> print(...)
one two three
Lua stores arguments in a table. This table is the "arg" table. You can access the passed arguments inside using arg[1], arg[2], ...
arg[0] is the name of the lua program. arg[1] is the first argument passed, arg[2] is the second argument passed and so on...
If you run file.lua in cmd of freeswitch
freeswitch> luarun prog.lua arg1
You can use prog.lua:
#print(argv[1])
And run: $lua prog.lua arg1 (run in script folder)
You can use prong.lua:
#print(arg[1])
For OP and future visitors,
The library of Lua doesn't contain injecting a table 'args' into globals from command line switches. The program built from lua.c does, however, it is near impossible to use. The reason it is impossible to use is that program does NOT like multiple switches.
This is one of the reasons why my REPL/code executor LuaConsole was built. It gives you the table args as well as sends a tuple to the root pcall function (your executing environment is really a top-level pcall with probably an error handler attached). So both args[n] and local a = {...}; a[n] ...; all work properly with as many switches as you want. For example, -e to execute code from cmd line, -l to specify libraries, etc. It supports anything lua51 and up.
If you are having troubles with the program in the library, I highly suggest you check out https://www.github.com/tilkinsc/LuaConsole as it will save you the headache of dealing with a broken-feeling program. There are also other alternatives out there such as for web, fengari.
Hope this helps you!
How do I detect what command line arguments where given when a script is run with racket? That is, the equivalent of sys.argv in Python, args[] in Java, etc...
You have these choices (you can look them all up in the docs for more info):
current-command-line-arguments -- a vector holding the command line arguments
You can start a script with the -m flag, which will require the file and look for a provided main function, then apply it on the command-line arguments (as a list of strings)
Or you can require racket/cmdline which provides a macro that can be used to define several flags in a convenient way.