FREE TRIAL: Accelerate your app development career with Hacking with Swift+! >>

How do you get current directory for command line tools?

Forums > macOS

For fun, I'm writing a Swift command line tool for macOS. I want to pass in an argument like "foo.txt", which would be a text file in the current directory. I'm unclear on how the app will know what directory the shell is currently in. How do you pass the current directory to the command line apps implicity so the user does not have to specify them?

   

I think FileManager.currentDirectoryPath is what you are looking for.

1      

Thank you, David. I tested it and you are correct!

I disregarded currentDirectoryPath initially because the documentation says: "The path to the program’s current directory."

If I'd read the full docs, I'd have seen the warning: "This property reports the current working directory for the current process, not just the receiver."

Which is exactly what I wanted, thanks again!

   

Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Get started

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.