Emacs custom command line argument
Asked Answered
W

3

22

From the documentation I can see I can access command line arguments (command-line-args). I'd like to add my own arguments but Emacs complains at start up that it doesn't recognize them.

E.g.

emacs -my_argument

I get:

command-line-1: Unknown option `-my_argument'

What's a proper way to define my custom arguments and provide information to my Emacs session? Is there a way to pop an argument from a command line?

Wellinformed answered 21/1, 2010 at 19:36 Comment(0)
M
36

Add something like this to your ~/.emacs, ~/.emacs.el, or ~/.emacs.d/init.el file:

(defun my-argument-fn (switch)
  (message "i was passed -my_argument"))

(add-to-list 'command-switch-alist '("-my_argument" . my-argument-fn))

Then you can execute emacs -my_argument and it should print i was passed -my_argument to the minibuffer. You can find more information in the GNU elisp reference.

Mou answered 21/1, 2010 at 19:50 Comment(0)
C
12

As stated in another post you can add your custom switches to command-switch-alist and emacs will call the handler function for any matching switch passed in on the command line. However, this operation is done after your .emacs file has been evaluated. This is fine for most cases but you may wish for a command line argument to alter the execution path or behaviour of your .emacs evaluation; I often do this to enable/disable configuration chunks (mainly for debugging).

To achieve this you can read command-line-args and check for your switch manually and then delete it from the list, this will stop emacs complaining about an unknown argument.

(setq my-switch-found (member "-myswitch" command-line-args))
(setq command-line-args (delete "-myswitch" command-line-args))

Which can alter your .emacs evaluation like so:

(unless my-switch-found
  (message "Didn't find inhibit switch, loading some config.") 
  ...)

And you could build this into a single step:

;; This was written in SO text-box, not been tested.
(defun found-custom-arg (switch)
  (let ((found-switch (member switch command-line-args)))
    (setq command-line-args (delete switch command-line-args))
    found-switch))

(unless (found-custom-arg "-myswitch")
  (message "Loading config...")
  ...)
Contagion answered 31/10, 2010 at 22:10 Comment(1)
Tested the code marked "not been tested". It works exactly as described.Habitant
S
1

For those who are interested, here is a code snip to show how to process custom arguments in Emacs lisp. In this case, I am processing an argument --suffix / -S to variable _suffix.

I pulled the idea from a BSD-Lite Script Emacs script.

  (setq _suffix nil)

  ;; Process cli args
  (while command-line-args-left 
    (setq k (car command-line-args-left))
    (setq command-line-args-left (cdr command-line-args-left))
    (setq command-line-args (delete k command-line-args))
    (cond
      (or (string-equal k "--cs-suffix")
          (string-equal k "-S"))
        (setq _suffix (intern (car command-line-args-left)))
        (setq command-line-args-left (cdr command-line-args-left))
        (setq command-line-args (delete _suffix command-line-args))
      )))

This will roll through command-line-args-left and remove them all from command-line-args which will prevent Emacs from complaining.

Synchronize answered 6/4, 2022 at 21:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.