How can I pass arguments -q -d -Q -t -L -fg -bg --color etc?
Doing something like emacs --script -Q <scriptname> <arguments>
definetely will not pass arguments, which are used in emacs. So how to do it?
Based on your comment on Rafael Ibraim's answer, I'm adding this second answer, as I think my first answer is also mis-interpreting your question (and if so, you may wish to edit the question to provide clarification).
You can prevent Emacs from processing command line arguments using the usual approach of an 'empty' argument: --
So if you run this:
emacs --script (filename) -- -Q
Emacs will not eat the -Q
argument (or the --
in fact), and it will be available to your script. You can easily verify this with the following script:
(print argv)
":"; exec emacs --no-site-file --script "$0" -- "$@" # -*-emacs-lisp-*-
so this thing doing it's work perfectly. Many thanks again.
Commented
Jul 26, 2011 at 19:37
--
is needed to prevent Emacs from processing option arguments before the script executes, and (setq argv nil)
is needed before the script exits to prevent Emacs from processing non-option arguments as filenames, and visiting them.
argv
is to simply exit explicitly at the end of the script with (kill-emacs 0)
(assuming 0
is the appropriate status at that point), which ensures (a) that Emacs doesn't do anything else (processing of remaining arguments, or otherwise); and (b) that you guarantee which exit status your script is returning (which is something you should want to do in any case). Refer to lunaryorn.com/2014/08/12/emacs-script-pitfalls.html
If you run emacs in script mode, I would recommend to reset "argv" variable to nil in the end of your script, otherwise emacs will try interpret "argv" after script is finished.
Let's assuming you have a file named "test-emacs-script.el" with the following content:
#!/usr/bin/emacs --script
(print argv)
(setq argv nil)
Try running this script as "./test-emacs-script.el -a". If you run this script without resettings "argv" (last line in the script) then the output will be:
("-a")
Unknown option `-a'
Resetting "argv" gets rid of "unknown option" error message
./test-emacs-script.el -Q
is nil
. The trick of using the --
argument avoids that issue as well as the one you are addressing, so I believe your approach is only recommended if you don't actually want those arguments to reach your script.
--
is needed to prevent Emacs from processing option arguments before the script executes, and (setq argv nil)
is needed before the script exits to prevent Emacs from processing non-option arguments as filenames, and visiting them.
argv
is to simply exit explicitly at the end of the script with (kill-emacs 0)
(assuming 0
is the appropriate status at that point), which ensures (a) that Emacs doesn't do anything else (processing of remaining arguments, or otherwise); and (b) that you guarantee which exit status your script is returning (which is something you should want to do in any case). Refer to lunaryorn.com/2014/08/12/emacs-script-pitfalls.html
I think you have two options here:
command-line-args
. The only problem is that this variable probably doesn't do exactly what you want(from the manual):
Most options specify how to initialize Emacs, or set parameters for the Emacs session. We call them initial options. A few options specify things to do, such as loading libraries or calling Lisp functions. These are called action options. These and file names together are called action arguments. The action arguments are stored as a list of strings in the variable command-line-args. (Actually, when Emacs starts up, command-line-args contains all the arguments passed from the command line; during initialization, the initial arguments are removed from this list when they are processed, leaving only the action arguments.)
In other words, only action arguments will be available in command-line-args
, wich doesn't really help you much.
If you're typing the command manually at the shell there shouldn't be a problem (excepting that in your example your script name is -Q
), so I assume you're trying to create an executable script with emacs as the shebang command?
This is the best solution I've seen for creating portable executable elisp scripts which include additional arguments:
Emacs shell scripts - how to put initial options into the script?