13

I'm currently making a program (which requires some arguments) that runs on the terminal.

Now I would like to run this same program from Sublime Text, but I don't know how to pass parameters to the build before executing the program in Sublime Text.

Is there any option that I need to enable to specify the arguments?

Using Sublime Text 3 build 3035

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rafael
  • 3,081
  • 6
  • 32
  • 53

3 Answers3

17

You can create a new build system for sublime text and run your script with fixed arguments.

Create a new File in your Packages/User directory (CTRL-SHIFT-P --> "Browse Packages")

New File: Packages/User/my_build.sublime-build

with the following content:

{
   "cmd": ["python", "$file", "arg1", "arg2"]
}

(replace arg1,arg2 by your arguments - you can delete them or add more if you want)

Now restart sublime text and select your build system in the Menu: Tools --> Build System --> my_build. From now on, when you press CTRL-B your build system will be executed.

Don't forget to change it back to "Automatic" if you are working on other files or projects.

There are many options you can set in build files. Please refer to https://docs.sublimetext.io/guide/usage/build-systems.html

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
michaelkrisper
  • 707
  • 6
  • 8
  • 3
    [Better sollution](http://stackoverflow.com/questions/16490889/build-and-run-with-args-in-sublime-text-2) which is doesn't pollute the global Build System menu and easier to edit. – Dmitry Verhoturov Jan 21 '15 at 13:05
  • This is not a particularly great solution, for eg: it forces you to use only the pybuild that you configured Lets say you are using Conda build system to choose a venv on sublime that cannot be used/activated anymore The best solution is to use package control or just use the Command Line interface instead – Sandeep Anand Jul 03 '22 at 18:31
10

I find it easier to use a try catch with default arguments, Sublime's build system becomes annoying to manage. While you do fast paced dev you can just modify the arguments in the except statement.

import sys
try:
    if sys.argv[1]:
        Name = str(sys.argv[1])

except:
    print "no argument given - using DERP"
    Name = "DERP"
  • That doesn't really work when you want to force the user to make a decision about what argument to use. – André C. Andersen Jun 20 '16 at 22:20
  • Right, but if you don't need to force and are testing scripts that will be taking different kinds of arguments, this is very, very useful & far cleaner then the messy build system. I prefer this one. I have regression tests performed on 2 files. Various combinations of files are supplied to script. This really helps to define two test files. +1 – luci5r May 23 '18 at 00:46
  • Generally speaking, ability of passing parameters while building with ST seems very interesting. – khaz Jan 05 '23 at 16:24
0

The simplest solution is to import sys and to add to his list sys.argv the list of args you need.

Example:

#!/usr/bin/env python3

#-*- coding: utf-8 -*-

import numpy as np

#############here your python script

#at the end :

import sys

arglist=["Am Brunnen vor dem Tore",

"Da steht ein Lindenbaum",

"Ich träumt' in seinem Schatten",

"So manchen süßen Traum"] 

# append one arg or a list :

sys.argv.append(arglist)

#thats ALL.. as you see, sublime isn't involved, use it on any IDE.. 


if __name__ == "__main__" :

    import fire

    fire.Fire(main)
Eric Aya
  • 69,473
  • 35
  • 181
  • 253