Home > Delphi, Language research, Object Pascal, OP4JS, Smart Mobile Studio > Copas, Delphi script for Shell

Copas, Delphi script for Shell

October 21, 2015 Leave a comment Go to comments

Have you ever wished that there was some way to script the shell without resorting to archaic batch files, python or any of the other weird languages out there? Well, a while back I started to implement a special version of DWScript (Delphi web script) which does exactly that. It allows you to execute DWScript files directly from the shell powered by a rich set of OS level functionality.

I havent come up with a cool name for it yet so i dubbed it copas, short for “command-line object pascal”. Since DWScript is the parser engine we use with Smart Mobile Studio, the dialect is identical – and you can even use some of the Smart Pascal classes with it (no web-engine stuff naturally).

It's a start

It’s a start

If someone updates the DWScript repository for FreePascal to the latest, we can kiss python, node.js and all the other odd-ball scripting engines for shell/bash goodbye!

Why is this cool?

Being able to execute pascal scripts is not the hard part (since that is already covered by DWScript), the hard part is providing classes that allows you to interact with the operative system. Things like file creation, pipes, executing applications, service rights — these are not complex but rather time-consuming tasks.

Since time is not something I have these days, it will take some time before I release any source-code for this, but perhaps over Xmas I’ll have time to finish it. I started on this months ago but havent had time to do much work on it. But streams and a pipe client (for windows) is already in place. The plan is to compile with FireDAC (or morpheus) so you can connect directly to all major DB engines, create databases on the fly, copy data between engines, perform backup tasks with ease — well, the possibilities are endless and only limited by time and imagination.

But the benefits should be obvious:

  • creating scripts that deals with files and folders can now be done in a language you know and love
  • Full OOP support, including partial classes, interfaces and all the cool stuff Smart Pascal is currently offering
  • Script servers (REST, WebSocket etc)
  • Add smart mobile studio and you have a fully object pascal driven infrastructure.
  • Perfect for automation tasks

Well, no time to complete this now – but after Xmas I’ll share out the SVN repo.

  1. David Heffernan
    October 21, 2015 at 5:50 pm

    “Have you ever wished that there was some way to script the shell without resorting to archaic batch files, python or any of the other weird languages out there?”

    No. Python is perfect for this task. It’s not very good only to have one language.

    • Jon Lennart Aasenden
      October 22, 2015 at 8:25 am

      That involves learning python, which I for one dont have neither time nor interest in. I love object pascal in all it’s forms and for me, i’m much more effective using object pascal than say python, bash-script or any of the other scripting forms.

      A concrete example: Scripting a backup solution with fail-safe in case of errors in a dos script. No objects, no exception handling — you have to go back to goto() commands !
      The same chore was finished in record time using object pascal, because thats where we have our knowledge and experience.

      But, each to his own i guess. Whatever works for you

      • October 24, 2015 at 5:22 pm

        There was a talk a few years ago at a Perl conference in which Perl was compared to an island: information about modern Perl doesn’t get out to the rest of the world, and worse, new ideas don’t get onto the island. When users leave the island, they don’t come back.

        I’m with Warren on this – getting emotionally attached to a language to the point you won’t program in anything else, even in an area that language was designed for, is a bad idea. Warren has written an excellent blog post about this which argues that looking at other ways of doing things opens your mind. Elite hacker Eric S. Raymond has similarly advised that, for instance, learning LISP will make you a better programmer even if you never end up writing a single line of LISP. Just this week one of the Python core developers published an article about *27* languages that can make one a better Python programmer. He grouped languages into different categories – functional, event-driven, etc. – and suggested learning a language in each category would improve one’s programming skills. “One of the things we do as part of the Python core development process is to look at features we appreciate having available in other languages we have experience with, and see whether or not there is a way to adapt them to be useful in making Python code easier to both read and write. This means that learning another programming language that focuses more specifically on a given style of software development can help improve anyone’s understanding of that style of programming in the context of Python.”

        Other benefits of learning new languages include not being locked into a single platform/vendor and keeping one’s career options fluid (or in some cases, viable).

        >The same chore was finished in record time using object
        >pascal, because thats where we have our knowledge and

        But isn’t that a case of “if the only tool you have is a hammer, every problem begins to look like a nail”? Of course you finished faster than Python, because you don’t know any Python. Doesn’t PowerShell have many modern features, including objects?

        I recently automated a small task – getting a backlit keyboard to toggle its light on and off when I hit a certain key. To do so required running an external program and getting its output back to check the status of the lights and then sending the appropriate command. After consulting Stack Overflow, the process to get the output of an external program requires *10 variables*, zeroing a buffer, worrying about handles, etc. in Delphi. You can see the nightmare process here:


        In Python? ONE line of code. My entire program was four lines:

        from subprocess import check_output, call

        output = check_output([“xset”, “q”], universal_newlines=True)
        LED = “-led” if “Scroll Lock: on” in output else “led”

        call([“xset”, LED, “3”])

        Which would you have rather written? Interestingly, in just four lines we also see, in addition to the single-line convenience function to call programs, *four* additional features it would be nice to have in Delphi: fine-grained import control to avoid namespace pollution/mangling, named parameters (for optional parameters and clarity), ability to search for substrings via “in” and a ternary operator (the single line if…else). Looking at other languages can give one lots of ideas about ways to improve Delphi (and DWScript). Heck, I was the one who suggested to Eric Grange the use of “in” to check for a substring in a string for DWScript after I saw it in Python! See what benefits looking at other languages can bring? 🙂

        On top of that, my accounting software, database, spreadsheet, virtualization tool, IM client, bittorrent client, DVCS, ebook manager, blog generator, documentation tool, text editor, media manager, etc. all support Python scripting or plugin creation. Learning Python opened up a HUGE amount of potential for me, and I guarantee you it takes much less time to learn than to squeeze DWScript into the shell.

        You’re missing out on a lot if you don’t learn other languages. You can’t honestly say you love Delphi if you’ve only spent time with Delphi. It’s like me telling my only nephew “You’re my favorite nephew!” Now that he’s almost five I think he’s starting to catch on. 😉

  2. October 22, 2015 at 7:15 am

    This is really a cool initiative. I like DWScript very much and I use it is several of my applications. I was thinking of making a command line interface for it and you are making it. i’m defenitely interested by your project. Maybe I could contribute.

    • Jon Lennart Aasenden
      October 22, 2015 at 8:27 am

      I wont have much free time until after xmas, but then i’ll push it to git/svn and we can all work together on it. I also want to talk to eric about making a permanent DWS port to lazarus (the current port is ancient) so we can compile for all platforms. It would be perfect for my NAS or raspberry PI projects 🙂

  3. David Heffernan
    October 22, 2015 at 8:34 am

    It’s pretty limiting only to know a single language. It’s going to take you a lifetime to make something as powerful as Python.

    In my opinion, programmers that refuse to learn more than one language are handicapping themselves. The programmers that I know that know multiple languages are so much better for it.

    • Jon Lennart Aasenden
      October 28, 2015 at 1:48 pm

      Im not looking to replace anything, just makes sense that in a purely delphi based setup to also have the tools at our disposal to deal with that. And object pascal has plenty of features which are a bonus over other languages (and visa versa). People can use what they like, but now we can also batch in object pascal and solve stuff in object pascal.

      For instance, imagine you have a command-line utility for backups. A really great one. But you want to automate it and add stuff like date/time handling, incremental use — why spend ages doing this in dos-script when you can wrap it up in a day using object pascal?
      If you dont already know python or ruby that is.

      I think it’s nice to have a well stocked object pascal toolkit. I dont see the python groups giving much thought to object pascal, so why should we think about python when we can get the job done equally fast using our prefered language?

    • December 23, 2015 at 3:54 pm

      Why do you think and argue in terms of programmers? That’s your perspective. While it counts it’s not only one that does. Getting to know ‘another language’ is not only about expanding one’s horizon within the domain of computer science or IT. It’s about getting to know other paradigm’s too, not because knowing many paradigms does matter. If you develop only and you know one language only you run at risk to end up in a tunnel.

      Why should there be a need to make something as powerful as Python? Python does already exist.

      If you lock-in then you should not show mercy. In other cultures locking oneself’s in is called staying committed.

      Python is perfect for this job. That’s definitely true. Python simply made it’s way over the years without being pushed from the very beginning.

      Java and .net were very different from that perspective. .net was introduced by putting force on the IT organizations (everything .net) and Java was introduced and heavily praised because of offering a framework well designed with a focus on design patterns in order to attract the Smalltalk developers, later the ‘automatic’ threading in order attract the ‘VB developer’ was shifted into the focus, next SOAP services one standard (MS and Sun, WS*) and last but not least ‘unbreakable security out of the box …’. And both just succeeded because C/C++ was so broke. Java was more or less the first language/framework except for Delphi that offered serialization, one point C/C++ never really offered out of the box. Then this serialization feature was heavily used in CORBA. The big move was to SOAP/XML.

      UNIX is pretty different. What Jon offers is little Windows-tinted. First it’s his freedom to do so and we should be happy he is still heavily engaged in that area. Pascal does suffer from very much the same Austria does suffer. Being small does offer opportunities, which are not perceived and honored, because most of the discussions are about attracting people to big moves of the big and tiny steps of the bigger are also honored a lot more. From an objective perspective what Jon offers is a huge step for something great but not that big. I think Python people will very likely honor his efforts. The moment the professionalists invade a technology the charming is gone and you can be happy if a few professionals stay. The Python people are aware of that, I’m pretty sure. Until today they could manage this. in my definition a professional is talking about what he is doing and not the other way around. Python was simply and still is very attractive because you don’t have tons of competing things built on top in order to address shortcomings.

      That’s the privilege of the professionalist. Others call the process of invading commercialization. First you have the big rumble in the saloon and the next the barkeeper faces a broken broken furnishings.

  4. October 23, 2015 at 6:33 am

    Does it do object piping?

    • Jon Lennart Aasenden
      October 28, 2015 at 1:44 pm

      It does what DWScript does + the most common features. You can add JSON and persist that out if you like, or just use it for smaller chores.

  5. March 24, 2016 at 8:29 am

    Can’t wait to hear the progress. It;s a cool project.

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: