일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- ListView
- mysql
- logging
- algorithm
- Visual Studio
- string
- Process
- C#
- csharp
- Github
- IValueConverter
- programmers
- windows10
- convert
- nullable
- coding-test
- windows
- Binding
- dotNET
- WPF
- log
- Coding
- Microsoft
- .net
- File
- git
- chashtag
- commit
- tls
- 코딩테스트
Archives
- Today
- Total
CHashtag
[Node.js] Command Line 시작 시 인자 전달하기 (Arguments) 본문
반응형
안녕하세요.
오늘은 Node.js를 Command Line으로 실행할 때 그 뒤에 인자를 전달하는 방법에 대해 알아보도록 하겠습니다.
아래의 명령을 CMD에서 입력하였을 때 Hello와 Hi를 출력하는 프로그램을 만들어 보도록 하겠습니다.
node Index.js Hello Hi
Node.js 공식 문서에 의하면, 별도의 라이브러리 없이 process.argv를 사용하면 가능하다고 합니다.
(nodejs.org/docs/latest/api/process.html#process_process_argv)
이해를 돕기위해 간단한 예제를 보여드리도록 하겠습니다.
// Index.js
process.argv.forEach((val, index) => {
console.log(`${index}: ${val}`);
});
출력값은 다음과 같습니다.
0: node
1: Index.js
2: Hello
3: Hi
argv는 node와 index.js를 포함하기 때문에 이를 유의하며 작업하셔야 합니다.
또는 process.argv는 배열이기 때문에 직접 Index에 접근해도 무관합니다.
console.log(process.argv[2]);
// Hello
console.log(process.argv[4]);
// undefined
감사합니다.
반응형