2

GNU Smalltalk omits the script name in argv.

#!/usr/bin/env gst -f

| argv program |

argv := Smalltalk arguments.

(argv size) > 0 ifTrue: [
    program := argv at: 1.

    Transcript show: 'Program: ', program; cr.
] ifFalse: [
    Transcript show: 'argv = {}'; cr.
]

$ ./scriptname.st
argv = {}

I see two ways to get the script name:

  • Track down some Smalltalk method which returns the script name akin to Perl's variable $0.
  • Track down syntax for a multiline shebang and force GST to supply the scriptname as the first member of argv. Here's an example in Common Lisp.
mcandre
  • 22,868
  • 20
  • 88
  • 147
  • 1
    Post your questions to the GNU Smalltalk mailing list and you're quite likely to benefit from the knowledge and experience of those who implement GST http://smalltalk.gnu.org/community/ml – igouy Aug 07 '11 at 16:43

2 Answers2

1

It seems the best that can be done is use shebangs to force the script name to ARGV, then check whether Smalltalk getArgv: 1 ends with a hardcoded string.

Posted here and on Rosetta Code.

"exec" "gst" "-f" "$0" "$0" "$@"
"exit"

Object subclass: ScriptedMain [
    ScriptedMain class >> meaningOfLife [ ^42 ]
]

| main |

main := [
    Transcript show: 'Main: The meaning of life is ', ((ScriptedMain meaningOfLife) printString); cr.
].

(((Smalltalk getArgc) > 0) and: [ ((Smalltalk getArgv: 1) endsWith: 'scriptedmain.st') ]) ifTrue: [
    main value.
]
mcandre
  • 22,868
  • 20
  • 88
  • 147
1

You can ask the current method where it comes from: thisContext method methodSourceFile printNl.

Paolo Bonzini
  • 1,900
  • 15
  • 25
  • That's helpful, but it doesn't work well with scriptedmain (http://rosettacode.org/wiki/ScriptedMain#Smalltalk), because that doesn't tell you which file was run with `gst somefile.st`. – mcandre Aug 08 '11 at 19:53
  • Thanks for joining the GNU Smalltalk mailing list. When I Google my newbie questions I often see you asking the same questions in the mailing list archives. – mcandre Aug 09 '11 at 04:03