unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* How can i make executable guile file to process command line arguments?
@ 2022-08-18 14:01 Jacob Hrbek
  2022-08-18 14:26 ` Olivier Dion via General Guile related discussions
  2022-08-18 17:58 ` Zelphir Kaltstahl
  0 siblings, 2 replies; 3+ messages in thread
From: Jacob Hrbek @ 2022-08-18 14:01 UTC (permalink / raw)
  To: guile-user


[-- Attachment #1.1: Type: text/plain, Size: 1854 bytes --]

I have this file which i use to interact with a version controlled 
repository similar to a Makefile.

How can i make it work with e.g. `./file.scm echo wheee` -> Will execute 
the `(define (echo msg) ...)` and output `wheee` in the terminal?

FWIW preferably i want it to work like `./file.scm echo whee build` so 
that it would output `wheee` and then ran the `(define (build) ...)` 
task too..

#!/usr/bin/env sh
exec guile -s "$0" "$@"
!#
;;; This file is used to interact with the project

;;; ==== [START] TO BE INCLUDED IN A LIBRARY [START] ==== ;;;

;; Get path to the current filename
(define-public %repo-root (dirname (current-filename)))

;;; ==== [STOP] TO BE INCLUDED IN A LIBRARY [STOP] ==== ;;;

;; FIXME-QA(Krey): We need to define an alternative to `(define 
(task)...)` to be more workable and faster to write

;;; Tasks
;; DESIGN(Krey): Build the system configuration
(define (build)
   (system* "guix"
        "system"
        "build"
        (string-append %repo-root "/config.scm")))

;; DESIGN(Krey): Deploy the system configuration on the current system
(define (deploy)
   (system* "guix"
        "system"
        "reconfigure"
        (string-append %repo-root "/config.scm")))

;; DESIGN(Krey): Build the bare minimal image for rockpro64
(define (tools-minimal-rockpro64)
   (system* "guix"
        "system"
        "--system=aarch64-linux"
        "image"
        (string-append %file-root "/config.scm")))

;; Processing of cli arguments
(define (echo msg) (system* "echo" msg))

;; DESIGN(Krey): Expecting to process the command-line args here
;; HOTFIX(Krey): Didn't figure out how to do it yet, so currently 
hard-coding
; deploy

;(quote (cdr (command-line)))
;;(match (command-line) ((_ "build") (build)))


[-- Attachment #1.2: publickey - kreyren@rixotstudio.cz - 1677db82.asc --]
[-- Type: application/pgp-keys, Size: 713 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 249 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How can i make executable guile file to process command line arguments?
  2022-08-18 14:01 How can i make executable guile file to process command line arguments? Jacob Hrbek
@ 2022-08-18 14:26 ` Olivier Dion via General Guile related discussions
  2022-08-18 17:58 ` Zelphir Kaltstahl
  1 sibling, 0 replies; 3+ messages in thread
From: Olivier Dion via General Guile related discussions @ 2022-08-18 14:26 UTC (permalink / raw)
  To: Jacob Hrbek, guile-user

On Thu, 18 Aug 2022, Jacob Hrbek <kreyren@rixotstudio.cz> wrote:
> I have this file which i use to interact with a version controlled 
> repository similar to a Makefile.
>
> How can i make it work with e.g. `./file.scm echo wheee` -> Will execute 
> the `(define (echo msg) ...)` and output `wheee` in the terminal?

You can't directly call your command.  This is because the shebang is
fixed and thus you will always call the same procedure.  But you could
dispatch your commands in your entrypoint.

How about something like this:
--8<---------------cut here---------------start------------->8---
#!/usr/bin/env -S guile --no-auto-compile -e main -s
-*-Scheme-*-
!#

(use-modules
  (ice-9 match))

(define (echo msg)
  (display msg)
  (newline))

(define (usage prog)
  (format (current-output-port)
          "Usage: ~a COMMAND ARGS..."
          prog))

(define (main args)
  (exit
    (match (cdr args)
      (("echo" msg . rest) (echo msg) #t)
      (_ (usage (car args)) #f))))
--8<---------------cut here---------------end--------------->8---



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How can i make executable guile file to process command line arguments?
  2022-08-18 14:01 How can i make executable guile file to process command line arguments? Jacob Hrbek
  2022-08-18 14:26 ` Olivier Dion via General Guile related discussions
@ 2022-08-18 17:58 ` Zelphir Kaltstahl
  1 sibling, 0 replies; 3+ messages in thread
From: Zelphir Kaltstahl @ 2022-08-18 17:58 UTC (permalink / raw)
  To: Jacob Hrbek; +Cc: guile-user

Hello Jacob,

On 8/18/22 16:01, Jacob Hrbek wrote:
> I have this file which i use to interact with a version controlled repository 
> similar to a Makefile.
>
> How can i make it work with e.g. `./file.scm echo wheee` -> Will execute the 
> `(define (echo msg) ...)` and output `wheee` in the terminal?
>
> FWIW preferably i want it to work like `./file.scm echo whee build` so that it 
> would output `wheee` and then ran the `(define (build) ...)` task too..
>
> #!/usr/bin/env sh
> exec guile -s "$0" "$@"
> !#
> ;;; This file is used to interact with the project
>
> ;;; ==== [START] TO BE INCLUDED IN A LIBRARY [START] ==== ;;;
>
> ;; Get path to the current filename
> (define-public %repo-root (dirname (current-filename)))
>
> ;;; ==== [STOP] TO BE INCLUDED IN A LIBRARY [STOP] ==== ;;;
>
> ;; FIXME-QA(Krey): We need to define an alternative to `(define (task)...)` to 
> be more workable and faster to write
>
> ;;; Tasks
> ;; DESIGN(Krey): Build the system configuration
> (define (build)
>   (system* "guix"
>        "system"
>        "build"
>        (string-append %repo-root "/config.scm")))
>
> ;; DESIGN(Krey): Deploy the system configuration on the current system
> (define (deploy)
>   (system* "guix"
>        "system"
>        "reconfigure"
>        (string-append %repo-root "/config.scm")))
>
> ;; DESIGN(Krey): Build the bare minimal image for rockpro64
> (define (tools-minimal-rockpro64)
>   (system* "guix"
>        "system"
>        "--system=aarch64-linux"
>        "image"
>        (string-append %file-root "/config.scm")))
>
> ;; Processing of cli arguments
> (define (echo msg) (system* "echo" msg))
>
> ;; DESIGN(Krey): Expecting to process the command-line args here
> ;; HOTFIX(Krey): Didn't figure out how to do it yet, so currently hard-coding
> ; deploy
>
> ;(quote (cdr (command-line)))
> ;;(match (command-line) ((_ "build") (build)))

If you are looking for having command line arguments like --some-flag, 
--my-val=123 and so on, you might want to take a look at the examples in my 
repo: 
https://notabug.org/ZelphirKaltstahl/guile-examples/src/master/command-line-arguments. 
Maybe your use-case can be implemented using such command line arguments, 
instead of having "raw arguments".

Regards,
Zelphir

-- 
repositories: https://notabug.org/ZelphirKaltstahl




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-08-18 17:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-18 14:01 How can i make executable guile file to process command line arguments? Jacob Hrbek
2022-08-18 14:26 ` Olivier Dion via General Guile related discussions
2022-08-18 17:58 ` Zelphir Kaltstahl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).