Modulinos: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Common Lisp)
(Added newLISP)
Line 1: Line 1:
{{task|Basic language learning}}[[Category: UNIX Shell]]
{{task|Basic language learning}}[[Category: UNIX Shell]]
It is useful to be able to run a main() function only when a program is run directly. This is a central feature in programming scripts; the feature is called /scripted main/.
It is useful to be able to execute a main() function only when a program is run directly. This is a central feature in programming scripts; the feature is called /scripted main/.


Examples from [https://github.com/mcandre/scriptedmain GitHub].
Examples from [https://github.com/mcandre/scriptedmain GitHub].
Line 157: Line 157:
}
}
}</lang>
}</lang>

=={{header|newLISP}}==
newLISP lacks scripted main, but the feature is easily added.

<lang lisp>#!/usr/bin/env newlisp

(context 'SCRIPTED-MAIN)

(define (main)
(println "Directory: " (real-path))

(println "Program: " (main-args 1))

(println "Number of Args: " (length (main-args)))

(map (lambda (x) (println "Arg: " x)) (main-args))

(exit))

(if (find "scriptedmain" (main-args 1)) (main))

(context MAIN)</lang>

Revision as of 23:38, 3 March 2011

Task
Modulinos
You are encouraged to solve this task according to the task description, using any language you may know.

It is useful to be able to execute a main() function only when a program is run directly. This is a central feature in programming scripts; the feature is called /scripted main/.

Examples from GitHub.

C

C programs have scripted main by default; as long as main() is not included in the header file, this program's (empty) API is accessible by other C code.

<lang c>#include "scriptedmain.h"

  1. include <stdio.h>
  2. include <unistd.h>

int main(int argc, char **argv) { char cwd[1024]; getcwd(cwd, sizeof(cwd));

printf("Directory: %s\n", cwd);

printf("Program: %s\n", argv[0]);

printf("Number of Args: %d\n", argc);

int i; for (i = 0; i < argc; i++) { printf("Arg: %s\n", argv[i]); }

return 0; }</lang>


C++

C++ has scripted main by default.

<lang cpp>#include <iostream>

  1. include <unistd.h>

using namespace std;

int main(int argc, char **argv) { char cwd[1024]; getcwd(cwd, sizeof(cwd));

cout << "Directory: " << cwd << endl;

cout << "Program: " << argv[0] << endl;

cout << "Number of Args: " << argc << endl;

int i; for (i = 0; i < argc; i++) { cout << "Arg: " << argv[i] << endl; }

return 0; }</lang>

Common Lisp

Common Lisp has few standards for POSIX operation. Shebangs and command line arguments are hacks.

<lang lisp>#!/bin/bash

  1. |

exec clisp -q -q $0 $0 ${1+"$@"} exit |#

Usage
./scriptedmain.lisp
With help from Rainer Joswig
http://www.math.utexas.edu/pipermail/maxima/2007/006523.html

(defun main (args)

(format t "Directory: ~a~%"
 #+clisp (ext:cd)
 #+lucid (working-directory)
 #+allegro (excl:current-directory)
 #+sbcl (progn *default-pathname-defaults*)
 #+(or :cmucl :scl) (ext:default-directory)
 #+lispworks (hcl:get-working-directory)
)
(format t "Program: ~a~%" (car args))
(format t "Number of Args: ~a~%" (length args))
(loop for arg in args do
 (format t "Arg: ~a~%" arg))
(quit))
With help from Francois-Rene Rideau
http://tinyurl.com/cli-args

(let ((args

      #+clisp ext:*args*
      #+sbcl sb-ext:*posix-argv*
      #+clozure (ccl::command-line-arguments)
      #+gcl si:*command-args*
      #+ecl (loop for i from 0 below (si:argc) collect (si:argv i))
      #+cmu extensions:*command-line-strings*
      #+allegro (sys:command-line-arguments)
      #+lispworks sys:*line-arguments-list*
    ))
 (if (member (pathname-name *load-truename*)
             args
             :test #'(lambda (x y) (search x y :test #'equalp)))
   (main args)))</lang>

Erlang

Erlang has scripted main by default.

<lang erlang>-module(scriptedmain). -import(lists, [map/2]).

main(Args) -> io:format("Directory: ~s~n", [filename:absname("")]), io:format("Program: ~s~n", [?FILE]), io:format("Number of Args: ~w~n", [length(Args)]), map (fun(Arg) -> io:format("Arg: ~s~n", [Arg]) end, Args).</lang>

Haskell

Haskell has scripted main by default.

<lang haskell>#!/usr/bin/env runhaskell

module ScriptedMain where

import System.Directory (getCurrentDirectory) import System (getProgName, getArgs) import Control.Monad (mapM_)

main :: IO () main = do directory <- getCurrentDirectory program <- getProgName args <- getArgs

putStrLn $ "Directory: " ++ directory putStrLn $ "Program: " ++ program putStrLn $ "Number of Args: " ++ (show . length) args mapM_ (\x -> putStrLn $ "Arg: " ++ x) args</lang>

Java

Java has scripted main by default.

<lang java>public class ScriptedMain { public static void main(String[] args) { System.out.println("Directory: " + ScriptedMain.class.getProtectionDomain().getCodeSource().getLocation().getPath());

System.out.println("Program: " + ScriptedMain.class.getName());

System.out.println("Number of Args: " + args.length);

for (int i = 0; i < args.length; i++) { System.out.println("Arg: " + args[i]); } } }</lang>

newLISP

newLISP lacks scripted main, but the feature is easily added.

<lang lisp>#!/usr/bin/env newlisp

(context 'SCRIPTED-MAIN)

(define (main) (println "Directory: " (real-path))

(println "Program: " (main-args 1))

(println "Number of Args: " (length (main-args)))

(map (lambda (x) (println "Arg: " x)) (main-args))

(exit))

(if (find "scriptedmain" (main-args 1)) (main))

(context MAIN)</lang>