WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

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?

3      

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

4      

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!

3      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.