Difference between revisions of "Shell (API)"

From ComputerCraft Wiki
Jump to: navigation, search
(renamed shell.programs optional argument)
m (Changed variable names to maintain consistency)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
The shell API allows you to interface with the [http://en.wikipedia.org/wiki/Shell_%28computing%29 shell].
+
{{lowercase}}
 +
The shell API allows you to interface with ComputerCraft's [http://en.wikipedia.org/wiki/Shell_%28computing%29 shell] - [[CraftOS]]. The shell API is only available when programs are ran from the shell or using [[shell.run]]/[[shell.openTab]].
 +
 
 +
Note that this "API" is not loaded into _G using [[os.loadAPI]]() - instead, it's loaded by [[shell|the shell script itself]] into a separate user environment, one which also holds [http://lua-users.org/wiki/ScopeTutorial global variables] for any other scripts the user executes. [[multishell (API)|The multishell API]] is handled in the same manner. They can thus be easily sandboxed (allowing for multiple shells to run on the same system at once - the APIs for each being able to return relevant results for each instance), though it does mean other APIs can't refer to these functions (as they lack access to the environment table dedicated to user scripts).
  
 
==API==
 
==API==
{| class="wikitable"
+
 
! width="100px" | Returns
+
{{API table|shell|image=Grid disk.png|2=
! width="200px" | Method name
+
 
! Description
+
{{API table/row
|-
+
|
+
 
|[[shell.exit]]()
 
|[[shell.exit]]()
 +
|{{type|nil}}
 
|Exits the current shell.
 
|Exits the current shell.
|-
+
|odd}}
|{{type|string}} directory
+
 
 +
{{API table/row
 
|[[shell.dir]]()
 
|[[shell.dir]]()
|Returns the directory.
+
|{{type|string}} directory
|-
+
|Returns the path to the working directory.}}
|
+
 
 +
{{API table/row
 
|[[shell.setDir]]({{type|string}} path)
 
|[[shell.setDir]]({{type|string}} path)
|Sets the directory.
+
|{{type|nil}}
|-
+
|Sets the working directory.
|{{type|string}} path
+
|odd}}
 +
 
 +
{{API table/row
 
|[[shell.path]]()
 
|[[shell.path]]()
|Returns the path.
+
|{{type|string}} path
|-
+
|Returns the path.}}
|
+
 
 +
{{API table/row
 
|[[shell.setPath]]({{type|string}} path)
 
|[[shell.setPath]]({{type|string}} path)
 +
|{{type|nil}}
 
|Sets the path.
 
|Sets the path.
|-
+
|odd}}
|{{type|string}} path
+
 
|[[shell.resolve]]({{type|string}} localpath)
+
{{API table/row
|Resolves a local path to an absolute path.
+
|[[shell.resolve]]({{type|string}} localPath)
|-
+
|{{type|string}} absolutePath
|{{type|string}} path
+
|Resolves a local path to an absolute path.}}
 +
 
 +
{{API table/row
 
|[[shell.resolveProgram]]({{type|string}} name)
 
|[[shell.resolveProgram]]({{type|string}} name)
 +
|{{type|string}} absolutePath
 
|Resolves the absolute path to the program whose name you provided.
 
|Resolves the absolute path to the program whose name you provided.
|-
+
|odd}}
|{{type|table}} aliases
+
 
 +
{{API table/row
 
|[[shell.aliases]]()
 
|[[shell.aliases]]()
|Returns aliases.
+
|{{type|table}} aliases
|-
+
|Returns aliases.}}
|
+
 
 +
{{API table/row
 
|[[shell.setAlias]]({{type|string}} alias, {{type|string}} program)
 
|[[shell.setAlias]]({{type|string}} alias, {{type|string}} program)
|Sets an alias.
+
|{{type|nil}}
|-
+
|Sets an alias for ''program''.
|
+
|odd}}
 +
 
 +
{{API table/row
 
|[[shell.clearAlias]]({{type|string}} alias)
 
|[[shell.clearAlias]]({{type|string}} alias)
|Clears an alias.
+
|{{type|nil}}
|-
+
|Clears an alias.}}
|{{type|table}} programs
+
 
 +
{{API table/row
 
|[[shell.programs]]([<nowiki/>{{type|boolean}} showHidden])
 
|[[shell.programs]]([<nowiki/>{{type|boolean}} showHidden])
|Returns a {{type|table}} of programs.
+
|{{type|table}} programs
|-
+
|Returns a table of files in the current directory and in all paths in [[shell.path]].
|{{type|boolean}} success
+
|odd}}
|[[shell.run]]({{type|string}} command [, {{type|string}} args1, {{type|string}} args2, ...])
+
 
|Runs a program.
+
{{API table/row
|-
+
|{{type|string}} path
+
 
|[[shell.getRunningProgram]]()
 
|[[shell.getRunningProgram]]()
|Returns the absolute path to the currently-executing program.
+
|{{type|string}} path
|-
+
|Returns the absolute path to the currently-executing program.}}
|{{type|number}} newTabID
+
 
 +
{{API table/row
 +
|[[shell.run]]({{type|string}} command [, {{type|string}} args1, {{type|string}} args2, ...])
 +
|{{type|boolean}} success
 +
|Runs a command (program).
 +
|odd}}
 +
 
 +
{{API table/row
 
|[[shell.openTab]]({{type|string}} command [, {{type|string}} args1, {{type|string}} args2, ...])
 
|[[shell.openTab]]({{type|string}} command [, {{type|string}} args1, {{type|string}} args2, ...])
|Runs a program in another [[multishell]] tab. ''Requires version 1.6 or newer and an advanced system.''
+
|{{type|number}} tabID
|-
+
|Runs a program in another [[multishell]] tab. ''Requires version 1.6 or newer and an advanced system.''}}
|
+
 
 +
{{API table/row
 
|[[shell.switchTab]]({{type|number}} tabID)
 
|[[shell.switchTab]]({{type|number}} tabID)
 +
|{{type|nil}}
 
|Switches the [[multishell]] tab to tab with the given ID.  ''Requires version 1.6 or newer and an advanced system.''
 
|Switches the [[multishell]] tab to tab with the given ID.  ''Requires version 1.6 or newer and an advanced system.''
|}
+
|odd}}
 +
 
 +
{{API table/row
 +
|[[shell.complete]]({{type|string}} prefix)
 +
|{{type|table}} completionList
 +
|Given a partial command line, returns a list of suffixes that could potentially be used to complete it.  ''Requires version 1.74 or newer.''}}
 +
 
 +
{{API table/row
 +
|[[shell.completeProgram]]({{type|string}} prefix)
 +
|{{type|table}} completionList
 +
|Given a partial script / directory path, returns a list of suffixes that could potentially be used to complete it, including [[shell.aliases|alias]] and [[shell.path|path]] matches.  ''Requires version 1.74 or newer.''
 +
|odd}}
 +
 
 +
{{API table/row
 +
|[[shell.setCompletionFunction]]({{type|string}} path, {{type|function}} completionFunction)
 +
|{{type|nil}}
 +
|Registers a function that determines how [[shell.complete]]() handles completion behavior for a given script. ''Requires version 1.74 or newer.''}}
 +
 
 +
{{API table/row
 +
|[[shell.getCompletionInfo]]()
 +
|{{type|table}} completionFunctions
 +
|Returns a pointer to the table containing functions registered by [[shell.setCompletionFunction]]() for use with [[shell.complete]](). ''Requires version 1.74 or newer.''
 +
|odd}}
 +
 
 +
}}
  
 
Note that [[shell.run]] and [[shell.openTab]] concatenate any arguments they are given (with single space separator characters) before parsing the command, and as of ComputerCraft version 1.45, now also support a single string containing ''all'' parameters.
 
Note that [[shell.run]] and [[shell.openTab]] concatenate any arguments they are given (with single space separator characters) before parsing the command, and as of ComputerCraft version 1.45, now also support a single string containing ''all'' parameters.
  
 
[[Category:APIs]]
 
[[Category:APIs]]

Latest revision as of 12:31, 14 August 2015

The shell API allows you to interface with ComputerCraft's shell - CraftOS. The shell API is only available when programs are ran from the shell or using shell.run/shell.openTab.

Note that this "API" is not loaded into _G using os.loadAPI() - instead, it's loaded by the shell script itself into a separate user environment, one which also holds global variables for any other scripts the user executes. The multishell API is handled in the same manner. They can thus be easily sandboxed (allowing for multiple shells to run on the same system at once - the APIs for each being able to return relevant results for each instance), though it does mean other APIs can't refer to these functions (as they lack access to the environment table dedicated to user scripts).

API

Grid disk.png  shell (API)
Function Return values Description
shell.exit() nil Exits the current shell.
shell.dir() string directory Returns the path to the working directory.
shell.setDir(string path) nil Sets the working directory.
shell.path() string path Returns the path.
shell.setPath(string path) nil Sets the path.
shell.resolve(string localPath) string absolutePath Resolves a local path to an absolute path.
shell.resolveProgram(string name) string absolutePath Resolves the absolute path to the program whose name you provided.
shell.aliases() table aliases Returns aliases.
shell.setAlias(string alias, string program) nil Sets an alias for program.
shell.clearAlias(string alias) nil Clears an alias.
shell.programs([boolean showHidden]) table programs Returns a table of files in the current directory and in all paths in shell.path.
shell.getRunningProgram() string path Returns the absolute path to the currently-executing program.
shell.run(string command [, string args1, string args2, ...]) boolean success Runs a command (program).
shell.openTab(string command [, string args1, string args2, ...]) number tabID Runs a program in another multishell tab. Requires version 1.6 or newer and an advanced system.
shell.switchTab(number tabID) nil Switches the multishell tab to tab with the given ID. Requires version 1.6 or newer and an advanced system.
shell.complete(string prefix) table completionList Given a partial command line, returns a list of suffixes that could potentially be used to complete it. Requires version 1.74 or newer.
shell.completeProgram(string prefix) table completionList Given a partial script / directory path, returns a list of suffixes that could potentially be used to complete it, including alias and path matches. Requires version 1.74 or newer.
shell.setCompletionFunction(string path, function completionFunction) nil Registers a function that determines how shell.complete() handles completion behavior for a given script. Requires version 1.74 or newer.
shell.getCompletionInfo() table completionFunctions Returns a pointer to the table containing functions registered by shell.setCompletionFunction() for use with shell.complete(). Requires version 1.74 or newer.

Note that shell.run and shell.openTab concatenate any arguments they are given (with single space separator characters) before parsing the command, and as of ComputerCraft version 1.45, now also support a single string containing all parameters.