Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters to a build in Sublime Text 3?

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

like image 781
Rafael Avatar asked May 17 '13 21:05

Rafael


2 Answers

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"
like image 90
Kieran Inductance D Avatar answered Oct 03 '22 19:10

Kieran Inductance D


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

like image 29
michaelkrisper Avatar answered Oct 03 '22 19:10

michaelkrisper